diff --git a/core/modules/layout_builder/src/Annotation/SectionStorage.php b/core/modules/layout_builder/src/Annotation/SectionStorage.php index 7e0cbc1791..73924be8ad 100644 --- a/core/modules/layout_builder/src/Annotation/SectionStorage.php +++ b/core/modules/layout_builder/src/Annotation/SectionStorage.php @@ -23,7 +23,7 @@ class SectionStorage extends Plugin { public $id; /** - * The plugin weight. + * The plugin weight, optional (defaults to 0). * * When an entity with layout is rendered, section storage plugins are * checked, in order of their weight, to determine which one should be used @@ -34,15 +34,15 @@ class SectionStorage extends Plugin { public $weight = 0; /** - * Any required context definitions. + * Any required context definitions, optional. * * When an entity with layout is rendered, all section storage plugins which * match a particular set of contexts are checked, in order of their weight, * to determine which plugin should be used to render the layout. * - * @see \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::getRuntimeSections() - * * @var array + * + * @see \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::getRuntimeSections() */ public $context = []; diff --git a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php index 781cbad31d..d91d08575d 100644 --- a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php +++ b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php @@ -5,12 +5,11 @@ use Drupal\Core\Entity\Entity\EntityViewDisplay as BaseEntityViewDisplay; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\FieldableEntityInterface; -use Drupal\Core\Plugin\Context\Context; -use Drupal\Core\Plugin\Context\ContextDefinition; use Drupal\Core\Plugin\Context\EntityContext; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; +use Drupal\layout_builder\LayoutEntityHelperTrait; use Drupal\layout_builder\Section; use Drupal\layout_builder\SectionComponent; use Drupal\layout_builder\SectionStorage\SectionStorageTrait; @@ -25,6 +24,7 @@ */ class LayoutBuilderEntityViewDisplay extends BaseEntityViewDisplay implements LayoutEntityDisplayInterface { + use LayoutEntityHelperTrait; use SectionStorageTrait; /** @@ -269,16 +269,7 @@ protected function getRuntimeSections(FieldableEntityInterface $entity) { $sections = NULL; if ($this->isOverridable()) { - /** @var \Drupal\layout_builder\SectionStorageInterface $storage */ - $storage = \Drupal::service('plugin.manager.layout_builder.section_storage') - ->loadFromContext([ - 'entity' => new Context(ContextDefinition::create('entity'), $entity), - 'view_mode' => new Context(ContextDefinition::create('string'), $this->getMode()), - ]); - - if ($storage) { - $sections = $storage->getSections(); - } + $sections = $this->getEntitySections($entity, $this->getMode()); } // If we don't have a section list yet (i.e., no section storage plugin // was able to derive a section list from context, or this display is not diff --git a/core/modules/layout_builder/src/EventSubscriber/SetInlineBlockDependency.php b/core/modules/layout_builder/src/EventSubscriber/SetInlineBlockDependency.php index edc05f83dd..4ea6259860 100644 --- a/core/modules/layout_builder/src/EventSubscriber/SetInlineBlockDependency.php +++ b/core/modules/layout_builder/src/EventSubscriber/SetInlineBlockDependency.php @@ -127,7 +127,8 @@ protected function getInlineBlockDependency(BlockContentInterface $block_content /** @var \Drupal\layout_builder\InlineBlockUsage $usage */ $layout_entity_storage = $this->entityTypeManager->getStorage($layout_entity_info->layout_entity_type); $layout_entity = $layout_entity_storage->load($layout_entity_info->layout_entity_id); - if ($this->isLayoutCompatibleEntity($layout_entity)) { + // @todo Pass 'default' until resolving https://www.drupal.org/node/3008924. + if ($this->isLayoutCompatibleEntity($layout_entity, 'default')) { if ($this->isBlockRevisionUsedInEntity($layout_entity, $block_content)) { return $layout_entity; } @@ -149,7 +150,8 @@ protected function getInlineBlockDependency(BlockContentInterface $block_content * layout entity. */ protected function isBlockRevisionUsedInEntity(EntityInterface $layout_entity, BlockContentInterface $block_content) { - $sections_blocks_revision_ids = $this->getInlineBlockRevisionIdsInSections($this->getEntitySections($layout_entity)); + // @todo Pass 'default' until resolving https://www.drupal.org/node/3008924. + $sections_blocks_revision_ids = $this->getInlineBlockRevisionIdsInSections($this->getEntitySections($layout_entity, 'default')); return in_array($block_content->getRevisionId(), $sections_blocks_revision_ids); } diff --git a/core/modules/layout_builder/src/InlineBlockEntityOperations.php b/core/modules/layout_builder/src/InlineBlockEntityOperations.php index 7e64b83cf1..3469fca493 100644 --- a/core/modules/layout_builder/src/InlineBlockEntityOperations.php +++ b/core/modules/layout_builder/src/InlineBlockEntityOperations.php @@ -6,7 +6,9 @@ use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Entity\RevisionableInterface; +use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface; use Drupal\layout_builder\Plugin\Block\InlineBlock; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -85,10 +87,12 @@ protected function removeUnusedForEntityOnSave(EntityInterface $entity) { if ($entity->isNew() || !isset($entity->original) || $entity instanceof RevisionableInterface) { return; } - $sections = $this->getEntitySections($entity); + // @todo Use 'default' until resolving https://www.drupal.org/node/3008924. + $view_mode = 'default'; + $sections = $this->getEntitySections($entity, $view_mode); // If this is a layout override and there are no sections then it is a new // override. - if ($this->isEntityUsingFieldOverride($entity) && empty($sections)) { + if ($this->isEntityUsingFieldOverride($entity, $view_mode) && empty($sections)) { return; } @@ -108,8 +112,10 @@ protected function removeUnusedForEntityOnSave(EntityInterface $entity) { * The block content IDs that were removed. */ protected function getRemovedBlockIds(EntityInterface $entity) { - $original_sections = $this->getEntitySections($entity->original); - $current_sections = $this->getEntitySections($entity); + // @todo Use 'default' until resolving https://www.drupal.org/node/3008924. + $view_mode = 'default'; + $original_sections = $this->getEntitySections($entity->original, $view_mode); + $current_sections = $this->getEntitySections($entity, $view_mode); // Avoid un-needed conversion from revision IDs to block content IDs by // first determining if there are any revisions in the original that are not // also in the current sections. @@ -132,11 +138,28 @@ protected function getRemovedBlockIds(EntityInterface $entity) { * The parent entity. */ public function handleEntityDelete(EntityInterface $entity) { - if ($this->isLayoutCompatibleEntity($entity)) { + // @todo Pass 'default' until resolving https://www.drupal.org/node/3008924. + if ($this->isLayoutCompatibleEntity($entity, 'default')) { $this->usage->removeByLayoutEntity($entity); } } + /** + * {@inheritdoc} + */ + protected function isLayoutCompatibleEntity(EntityInterface $entity, $view_mode) { + // @todo Hardcode these checks to avoid https://www.drupal.org/node/3008943. + return $entity instanceof LayoutEntityDisplayInterface || $this->isEntityUsingFieldOverride($entity, $view_mode); + } + + /** + * {@inheritdoc} + */ + protected function isEntityUsingFieldOverride(EntityInterface $entity, $view_mode) { + // @todo Hardcode these checks to avoid https://www.drupal.org/node/3008943. + return $entity instanceof FieldableEntityInterface && $entity->hasField('layout_builder__layout'); + } + /** * Handles saving a parent entity. * @@ -144,15 +167,17 @@ public function handleEntityDelete(EntityInterface $entity) { * The parent entity. */ public function handlePreSave(EntityInterface $entity) { - if (!$this->isLayoutCompatibleEntity($entity)) { + // @todo Use 'default' until resolving https://www.drupal.org/node/3008924. + $view_mode = 'default'; + if (!$this->isLayoutCompatibleEntity($entity, $view_mode)) { return; } $duplicate_blocks = FALSE; - if ($sections = $this->getEntitySections($entity)) { - if ($this->isEntityUsingFieldOverride($entity)) { + if ($sections = $this->getEntitySections($entity, $view_mode)) { + if ($this->isEntityUsingFieldOverride($entity, $view_mode)) { if (!$entity->isNew() && isset($entity->original)) { - if (empty($this->getEntitySections($entity->original))) { + if (empty($this->getEntitySections($entity->original, $view_mode))) { // If there were no sections in the original entity then this is a // new override from a default and the blocks need to be duplicated. $duplicate_blocks = TRUE; diff --git a/core/modules/layout_builder/src/LayoutEntityHelperTrait.php b/core/modules/layout_builder/src/LayoutEntityHelperTrait.php index 9124027542..907b8018f3 100644 --- a/core/modules/layout_builder/src/LayoutEntityHelperTrait.php +++ b/core/modules/layout_builder/src/LayoutEntityHelperTrait.php @@ -4,8 +4,8 @@ use Drupal\Component\Plugin\DerivativeInspectionInterface; use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Entity\FieldableEntityInterface; -use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface; +use Drupal\Core\Plugin\Context\Context; +use Drupal\Core\Plugin\Context\ContextDefinition; /** * Methods to help with entities using the layout builder. @@ -19,12 +19,14 @@ * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity to check. + * @param string $view_mode + * The view mode. * * @return bool * TRUE if the entity can have a layout otherwise FALSE. */ - protected function isLayoutCompatibleEntity(EntityInterface $entity) { - return $entity instanceof LayoutEntityDisplayInterface || $this->isEntityUsingFieldOverride($entity); + protected function isLayoutCompatibleEntity(EntityInterface $entity, $view_mode) { + return (bool) $this->getSectionStorageFromEntity($entity, $view_mode); } /** @@ -47,27 +49,42 @@ protected function getInlineBlockRevisionIdsInSections(array $sections) { return $revision_ids; } + /** + * Gets the section storage given an entity and view mode. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity. + * @param string $view_mode + * The view mode. + * + * @return \Drupal\layout_builder\SectionStorageInterface|null + * The section storage, if it exists. + */ + private function getSectionStorageFromEntity(EntityInterface $entity, $view_mode) { + /** @var \Drupal\layout_builder\SectionStorageInterface $storage */ + return \Drupal::service('plugin.manager.layout_builder.section_storage') + ->loadFromContext([ + 'view_mode' => new Context(ContextDefinition::create('string'), $view_mode), + // @todo Use EntityContext::fromEntity() after + // https://www.drupal.org/project/drupal/issues/3008431 is fixed. + 'entity' => new Context(ContextDefinition::create('entity'), $entity), + ]); + } + /** * Gets the sections for an entity if any. * - * @todo Replace this method with calls to the SectionStorageManagerInterface - * method for getting sections from an entity in - * https://www.drupal.org/node/2986403. - * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity. + * @param string $view_mode + * The view mode. * - * @return \Drupal\layout_builder\Section[]|null + * @return \Drupal\layout_builder\Section[] * The entity layout sections if available. */ - protected function getEntitySections(EntityInterface $entity) { - if ($entity instanceof LayoutEntityDisplayInterface) { - return $entity->getSections(); - } - elseif ($this->isEntityUsingFieldOverride($entity)) { - return $entity->get('layout_builder__layout')->getSections(); - } - return NULL; + protected function getEntitySections(EntityInterface $entity, $view_mode) { + $storage = $this->getSectionStorageFromEntity($entity, $view_mode); + return $storage ? $storage->getSections() : []; } /** @@ -97,12 +114,15 @@ protected function getInlineBlockComponents(array $sections) { * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity. + * @param string $view_mode + * The view mode. * * @return bool * TRUE if the entity is using a field for a layout override. */ - protected function isEntityUsingFieldOverride(EntityInterface $entity) { - return $entity instanceof FieldableEntityInterface && $entity->hasField('layout_builder__layout'); + protected function isEntityUsingFieldOverride(EntityInterface $entity, $view_mode) { + $storage = $this->getSectionStorageFromEntity($entity, $view_mode); + return $storage && $storage->getPluginId() === 'overrides'; } } diff --git a/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php b/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php index 811b344325..24c230055c 100644 --- a/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php +++ b/core/modules/layout_builder/src/Plugin/SectionStorage/DefaultsSectionStorage.php @@ -237,10 +237,8 @@ protected function getEntityTypes() { public function getContextsFromRoute($value, $definition, $name, array $defaults) { $contexts = []; - $id = $this->extractIdFromRoute($value, $definition, $name, $defaults); - if ($id) { - $section_list = $this->getSectionListFromId($id); - $contexts['entity'] = EntityContext::fromEntity($section_list); + if ($entity = $this->extractEntityFromRoute($value, $defaults)) { + $contexts['entity'] = EntityContext::fromEntity($entity); } return $contexts; } @@ -248,34 +246,29 @@ public function getContextsFromRoute($value, $definition, $name, array $defaults /** * {@inheritdoc} */ - protected function extractIdFromRoute($value, $definition, $name, array $defaults) { - if (is_string($value) && strpos($value, '.') !== FALSE) { - return $value; - } - + protected function extractEntityFromRoute($value, array $defaults) { // If a bundle is not provided but a value corresponding to the bundle key // is, use that for the bundle value. if (empty($defaults['bundle']) && isset($defaults['bundle_key']) && !empty($defaults[$defaults['bundle_key']])) { $defaults['bundle'] = $defaults[$defaults['bundle_key']]; } - if (!empty($defaults['entity_type_id']) && !empty($defaults['bundle']) && !empty($defaults['view_mode_name'])) { - return $defaults['entity_type_id'] . '.' . $defaults['bundle'] . '.' . $defaults['view_mode_name']; + if (is_string($value) && strpos($value, '.') !== FALSE) { + list($entity_type_id, $bundle, $view_mode) = explode('.', $value, 3); } - } - - /** - * {@inheritdoc} - */ - protected function getSectionListFromId($id) { - if (strpos($id, '.') === FALSE) { - throw new \InvalidArgumentException(sprintf('The "%s" ID for the "%s" section storage type is invalid', $id, $this->getStorageType())); + elseif (!empty($defaults['entity_type_id']) && !empty($defaults['bundle']) && !empty($defaults['view_mode_name'])) { + $entity_type_id = $defaults['entity_type_id']; + $bundle = $defaults['bundle']; + $view_mode = $defaults['view_mode_name']; + $value = "$entity_type_id.$bundle.$view_mode"; + } + else { + return NULL; } $storage = $this->entityTypeManager->getStorage('entity_view_display'); // If the display does not exist, create a new one. - if (!$display = $storage->load($id)) { - list($entity_type_id, $bundle, $view_mode) = explode('.', $id, 3); + if (!$display = $storage->load($value)) { $display = $storage->create([ 'targetEntityType' => $entity_type_id, 'bundle' => $bundle, diff --git a/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php b/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php index 5eda1331bc..4eec93230b 100644 --- a/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php +++ b/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php @@ -109,9 +109,7 @@ public function getStorageId() { public function getContextsFromRoute($value, $definition, $name, array $defaults) { $contexts = []; - $id = $this->extractIdFromRoute($value, $definition, $name, $defaults); - if ($id) { - $entity = $this->getSectionListFromId($id)->getEntity(); + if ($entity = $this->extractEntityFromRoute($value, $defaults)) { $contexts['entity'] = EntityContext::fromEntity($entity); } return $contexts; @@ -120,30 +118,22 @@ public function getContextsFromRoute($value, $definition, $name, array $defaults /** * {@inheritdoc} */ - protected function extractIdFromRoute($value, $definition, $name, array $defaults) { + protected function extractEntityFromRoute($value, array $defaults) { if (strpos($value, '.') !== FALSE) { - return $value; + list($entity_type_id, $entity_id) = explode('.', $value, 2); } - - if (isset($defaults['entity_type_id']) && !empty($defaults[$defaults['entity_type_id']])) { + elseif (isset($defaults['entity_type_id']) && !empty($defaults[$defaults['entity_type_id']])) { $entity_type_id = $defaults['entity_type_id']; $entity_id = $defaults[$entity_type_id]; - return $entity_type_id . '.' . $entity_id; } - } + else { + return NULL; + } - /** - * {@inheritdoc} - */ - protected function getSectionListFromId($id) { - if (strpos($id, '.') !== FALSE) { - list($entity_type_id, $entity_id) = explode('.', $id, 2); - $entity = $this->entityTypeManager->getStorage($entity_type_id)->load($entity_id); - if ($entity instanceof FieldableEntityInterface && $entity->hasField('layout_builder__layout')) { - return $entity->get('layout_builder__layout'); - } + $entity = $this->entityTypeManager->getStorage($entity_type_id)->load($entity_id); + if ($entity instanceof FieldableEntityInterface && $entity->hasField('layout_builder__layout')) { + return $entity; } - throw new \InvalidArgumentException(sprintf('The "%s" ID for the "%s" section storage type is invalid', $id, $this->getStorageType())); } /** diff --git a/core/modules/layout_builder/tests/modules/layout_builder_test/layout_builder_test.module b/core/modules/layout_builder/tests/modules/layout_builder_test/layout_builder_test.module index b00a40bbaa..8e9991f8cd 100644 --- a/core/modules/layout_builder/tests/modules/layout_builder_test/layout_builder_test.module +++ b/core/modules/layout_builder/tests/modules/layout_builder_test/layout_builder_test.module @@ -7,7 +7,7 @@ use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\EntityInterface; -use Drupal\layout_builder_test\Plugin\SectionStorage\OverridesSectionStorage; +use Drupal\layout_builder_test\Plugin\SectionStorage\TestOverridesSectionStorage; /** * Implements hook_plugin_filter_TYPE__CONSUMER_alter(). @@ -65,7 +65,7 @@ function layout_builder_test_node_view(array &$build, EntityInterface $entity, E */ function layout_builder_test_layout_builder_section_storage_alter(array &$storages) { /** @var \Drupal\layout_builder\SectionStorage\SectionStorageDefinition[] $storages */ - $storages['overrides']->setClass(OverridesSectionStorage::class); + $storages['overrides']->setClass(TestOverridesSectionStorage::class); $storages['overrides']->set('weight', -10); $storages['overrides_heavy'] = clone $storages['overrides']; diff --git a/core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/SectionStorage/OverridesSectionStorage.php b/core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/SectionStorage/TestOverridesSectionStorage.php similarity index 76% rename from core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/SectionStorage/OverridesSectionStorage.php rename to core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/SectionStorage/TestOverridesSectionStorage.php index 6a90765a6d..d4fd459343 100644 --- a/core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/SectionStorage/OverridesSectionStorage.php +++ b/core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/SectionStorage/TestOverridesSectionStorage.php @@ -2,9 +2,12 @@ namespace Drupal\layout_builder_test\Plugin\SectionStorage; -use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage as BaseOverridesSectionStorage; +use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage; -class OverridesSectionStorage extends BaseOverridesSectionStorage { +/** + * Provides a test override of section storage. + */ +class TestOverridesSectionStorage extends OverridesSectionStorage { /** * {@inheritdoc} diff --git a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php index a085fd2dcc..3c2301f62b 100644 --- a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php +++ b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php @@ -2,7 +2,6 @@ namespace Drupal\Tests\layout_builder\Functional; -use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay; use Drupal\node\Entity\Node; use Drupal\Tests\BrowserTestBase; use Drupal\views\Entity\View; diff --git a/core/modules/layout_builder/tests/src/Unit/DefaultsSectionStorageTest.php b/core/modules/layout_builder/tests/src/Unit/DefaultsSectionStorageTest.php index 73297d7f17..726d62dcab 100644 --- a/core/modules/layout_builder/tests/src/Unit/DefaultsSectionStorageTest.php +++ b/core/modules/layout_builder/tests/src/Unit/DefaultsSectionStorageTest.php @@ -52,7 +52,7 @@ protected function setUp() { 'id' => 'defaults', 'class' => DefaultsSectionStorage::class, ]); - $this->plugin = new TestDefaultsSectionStorage([], '', $definition, $this->entityTypeManager->reveal(), $entity_type_bundle_info->reveal(), $sample_entity_generator->reveal()); + $this->plugin = new DefaultsSectionStorage([], '', $definition, $this->entityTypeManager->reveal(), $entity_type_bundle_info->reveal(), $sample_entity_generator->reveal()); } /** @@ -84,58 +84,11 @@ public function testThirdPartySettings() { } /** - * @covers ::extractIdFromRoute + * @covers ::extractEntityFromRoute * - * @dataProvider providerTestExtractIdFromRoute + * @dataProvider providerTestExtractEntityFromRoute */ - public function testExtractIdFromRoute($expected, $value, array $defaults) { - $result = $this->plugin->extractIdFromRoute($value, [], 'the_parameter_name', $defaults); - $this->assertSame($expected, $result); - } - - /** - * Provides data for ::testExtractIdFromRoute(). - */ - public function providerTestExtractIdFromRoute() { - $data = []; - $data['with value'] = [ - 'foo.bar.baz', - 'foo.bar.baz', - [], - ]; - $data['empty value, without bundle'] = [ - 'my_entity_type.bundle_name.default', - '', - [ - 'entity_type_id' => 'my_entity_type', - 'view_mode_name' => 'default', - 'bundle_key' => 'my_bundle', - 'my_bundle' => 'bundle_name', - ], - ]; - $data['empty value, with bundle'] = [ - 'my_entity_type.bundle_name.default', - '', - [ - 'entity_type_id' => 'my_entity_type', - 'view_mode_name' => 'default', - 'bundle' => 'bundle_name', - ], - ]; - $data['without value, empty defaults'] = [ - NULL, - '', - [], - ]; - return $data; - } - - /** - * @covers ::getSectionListFromId - * - * @dataProvider providerTestGetSectionListFromId - */ - public function testGetSectionListFromId($success, $expected_entity_id, $value) { + public function testExtractEntityFromRoute($success, $expected_entity_id, $value, array $defaults = []) { if ($expected_entity_id) { $entity_storage = $this->prophesize(EntityStorageInterface::class); $entity_storage->load($expected_entity_id)->willReturn('the_return_value'); @@ -148,26 +101,48 @@ public function testGetSectionListFromId($success, $expected_entity_id, $value) $this->entityTypeManager->getStorage('entity_view_display')->shouldNotBeCalled(); } - if (!$success) { - $this->setExpectedException(\InvalidArgumentException::class); - } - - $result = $this->plugin->getSectionListFromId($value); + $method = new \ReflectionMethod($this->plugin, 'extractEntityFromRoute'); + $method->setAccessible(TRUE); + $result = $method->invoke($this->plugin, $value, $defaults); if ($success) { $this->assertEquals('the_return_value', $result); } + else { + $this->assertNull($result); + } } /** - * Provides data for ::testGetSectionListFromId(). + * Provides data for ::testExtractEntityFromRoute(). */ - public function providerTestGetSectionListFromId() { + public function providerTestExtractEntityFromRoute() { $data = []; $data['with value'] = [ TRUE, 'foo.bar.baz', 'foo.bar.baz', ]; + $data['empty value, without bundle'] = [ + TRUE, + 'my_entity_type.bundle_name.default', + '', + [ + 'entity_type_id' => 'my_entity_type', + 'view_mode_name' => 'default', + 'bundle_key' => 'my_bundle', + 'my_bundle' => 'bundle_name', + ], + ]; + $data['empty value, with bundle'] = [ + TRUE, + 'my_entity_type.bundle_name.default', + '', + [ + 'entity_type_id' => 'my_entity_type', + 'view_mode_name' => 'default', + 'bundle' => 'bundle_name', + ], + ]; $data['without value, empty defaults'] = [ FALSE, NULL, @@ -177,9 +152,9 @@ public function providerTestGetSectionListFromId() { } /** - * @covers ::getSectionListFromId + * @covers ::extractEntityFromRoute */ - public function testGetSectionListFromIdCreate() { + public function testExtractEntityFromRouteCreate() { $expected = 'the_return_value'; $value = 'foo.bar.baz'; $expected_create_values = [ @@ -195,7 +170,9 @@ public function testGetSectionListFromIdCreate() { $this->entityTypeManager->getDefinition('entity_view_display')->willReturn(new EntityType(['id' => 'entity_view_display'])); $this->entityTypeManager->getStorage('entity_view_display')->willReturn($entity_storage->reveal()); - $result = $this->plugin->getSectionListFromId($value); + $method = new \ReflectionMethod($this->plugin, 'extractEntityFromRoute'); + $method->setAccessible(TRUE); + $result = $method->invoke($this->plugin, $value, []); $this->assertSame($expected, $result); } @@ -429,21 +406,3 @@ public function testBuildRoutes() { } } - -class TestDefaultsSectionStorage extends DefaultsSectionStorage { - - /** - * {@inheritdoc} - */ - public function extractIdFromRoute($value, $definition, $name, array $defaults) { - return parent::extractIdFromRoute($value, $definition, $name, $defaults); - } - - /** - * {@inheritdoc} - */ - public function getSectionListFromId($id) { - return parent::getSectionListFromId($id); - } - -} diff --git a/core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php b/core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php index 6b20c60878..d3574be6dd 100644 --- a/core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php +++ b/core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php @@ -56,91 +56,46 @@ protected function setUp() { 'id' => 'overrides', 'class' => OverridesSectionStorage::class, ]); - $this->plugin = new TestOverridesSectionStorage([], 'overrides', $definition, $this->entityTypeManager->reveal(), $this->entityFieldManager->reveal()); + $this->plugin = new OverridesSectionStorage([], 'overrides', $definition, $this->entityTypeManager->reveal(), $this->entityFieldManager->reveal()); } /** - * @covers ::extractIdFromRoute + * @covers ::extractEntityFromRoute * - * @dataProvider providerTestExtractIdFromRoute + * @dataProvider providerTestExtractEntityFromRoute */ - public function testExtractIdFromRoute($expected, $value, array $defaults) { - $result = $this->plugin->extractIdFromRoute($value, [], 'the_parameter_name', $defaults); - $this->assertSame($expected, $result); - } - - /** - * Provides data for ::testExtractIdFromRoute(). - */ - public function providerTestExtractIdFromRoute() { - $data = []; - $data['with value, with layout'] = [ - 'my_entity_type.entity_with_layout', - 'my_entity_type.entity_with_layout', - [], - ]; - $data['with value, without layout'] = [ - NULL, - 'my_entity_type', - [], - ]; - $data['empty value, populated defaults'] = [ - 'my_entity_type.entity_with_layout', - '', - [ - 'entity_type_id' => 'my_entity_type', - 'my_entity_type' => 'entity_with_layout', - ], - ]; - $data['empty value, empty defaults'] = [ - NULL, - '', - [], - ]; - return $data; - } - - /** - * @covers ::getSectionListFromId - * - * @dataProvider providerTestGetSectionListFromId - */ - public function testGetSectionListFromId($success, $expected_entity_type_id, $id) { - $defaults['the_parameter_name'] = $id; - + public function testExtractEntityFromRoute($success, $expected_entity_type_id, $value, array $defaults = []) { if ($expected_entity_type_id) { $entity_storage = $this->prophesize(EntityStorageInterface::class); $entity_without_layout = $this->prophesize(FieldableEntityInterface::class); $entity_without_layout->hasField('layout_builder__layout')->willReturn(FALSE); - $entity_without_layout->get('layout_builder__layout')->shouldNotBeCalled(); $entity_storage->load('entity_without_layout')->willReturn($entity_without_layout->reveal()); $entity_with_layout = $this->prophesize(FieldableEntityInterface::class); $entity_with_layout->hasField('layout_builder__layout')->willReturn(TRUE); - $entity_with_layout->get('layout_builder__layout')->willReturn('the_return_value'); $entity_storage->load('entity_with_layout')->willReturn($entity_with_layout->reveal()); - $this->entityTypeManager->getStorage($expected_entity_type_id)->willReturn($entity_storage->reveal()); } else { $this->entityTypeManager->getStorage(Argument::any())->shouldNotBeCalled(); } - if (!$success) { - $this->setExpectedException(\InvalidArgumentException::class); - } - - $result = $this->plugin->getSectionListFromId($id); + $method = new \ReflectionMethod($this->plugin, 'extractEntityFromRoute'); + $method->setAccessible(TRUE); + $result = $method->invoke($this->plugin, $value, $defaults); if ($success) { - $this->assertEquals('the_return_value', $result); + $this->assertInstanceOf(FieldableEntityInterface::class, $result); + } + else { + $this->assertNull($result); } } /** - * Provides data for ::testGetSectionListFromId(). + * Provides data for ::testExtractEntityFromRoute(). */ - public function providerTestGetSectionListFromId() { + public function providerTestExtractEntityFromRoute() { $data = []; $data['with value, with layout'] = [ TRUE, @@ -152,6 +107,15 @@ public function providerTestGetSectionListFromId() { 'my_entity_type', 'my_entity_type.entity_without_layout', ]; + $data['empty value, populated defaults'] = [ + TRUE, + 'my_entity_type', + '', + [ + 'entity_type_id' => 'my_entity_type', + 'my_entity_type' => 'entity_with_layout', + ], + ]; $data['empty value, empty defaults'] = [ FALSE, NULL, @@ -388,21 +352,3 @@ public function testBuildRoutes() { } } - -class TestOverridesSectionStorage extends OverridesSectionStorage { - - /** - * {@inheritdoc} - */ - public function extractIdFromRoute($value, $definition, $name, array $defaults) { - return parent::extractIdFromRoute($value, $definition, $name, $defaults); - } - - /** - * {@inheritdoc} - */ - public function getSectionListFromId($id) { - return parent::getSectionListFromId($id); - } - -} diff --git a/core/modules/layout_builder/tests/src/Unit/SectionStorageManagerTest.php b/core/modules/layout_builder/tests/src/Unit/SectionStorageManagerTest.php index fcfb182965..10f41e937c 100644 --- a/core/modules/layout_builder/tests/src/Unit/SectionStorageManagerTest.php +++ b/core/modules/layout_builder/tests/src/Unit/SectionStorageManagerTest.php @@ -6,7 +6,6 @@ use Drupal\Component\Plugin\Factory\FactoryInterface; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Extension\ModuleHandlerInterface; -use Drupal\layout_builder\SectionListInterface; use Drupal\layout_builder\SectionStorage\SectionStorageManager; use Drupal\layout_builder\SectionStorageInterface; use Drupal\Tests\UnitTestCase;