diff --git a/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php b/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php index 1ec407fe7c..e64d57c05b 100644 --- a/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php +++ b/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php @@ -13,7 +13,6 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\Context\Context; use Drupal\Core\Plugin\Context\ContextDefinition; -use Drupal\Core\Plugin\Context\ContextRepositoryInterface; use Drupal\Core\Plugin\Context\EntityContext; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; @@ -75,23 +74,15 @@ class OverridesSectionStorage extends SectionStorageBase implements ContainerFac */ protected $entityRepository; - /** - * The context repository. - * - * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface - */ - protected $contextRepository; - /** * {@inheritdoc} */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, EntityRepositoryInterface $entity_repository, ContextRepositoryInterface $context_repository) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, EntityRepositoryInterface $entity_repository) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->entityTypeManager = $entity_type_manager; $this->entityFieldManager = $entity_field_manager; $this->entityRepository = $entity_repository; - $this->contextRepository = $context_repository; } /** @@ -168,8 +159,8 @@ public function extractIdFromRoute($value, $definition, $name, array $defaults) public function getSectionListFromId($id) { @trigger_error('\Drupal\layout_builder\SectionStorageInterface::getSectionListFromId() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. The section list should be derived from context. See https://www.drupal.org/node/3016262.', E_USER_DEPRECATED); if (strpos($id, '.') !== FALSE) { - list($entity_type_id, $entity_id) = explode('.', $id, 2); - $entity = $this->getActiveEntity($entity_type_id, $entity_id); + list($entity_type_id, $entity_id) = explode('.', $id); + $entity = $this->entityRepository->getActive($entity_type_id, $entity_id); if ($entity instanceof FieldableEntityInterface && $entity->hasField(static::FIELD_NAME)) { return $entity->get(static::FIELD_NAME); } @@ -218,7 +209,7 @@ private function extractEntityFromRoute($value, array $defaults) { return NULL; } - $entity = $this->getActiveEntity($entity_type_id, $entity_id); + $entity = $this->entityRepository->getActive($entity_type_id, $entity_id); if ($entity instanceof FieldableEntityInterface && $entity->hasField(static::FIELD_NAME)) { return $entity; } @@ -382,21 +373,4 @@ public function isApplicable(RefinableCacheableDependencyInterface $cacheability return $default_section_storage->isOverridable() && count($this); } - /** - * Gets the active entity for editing. - * - * @param string $entity_type_id - * The entity type id. - * @param string $entity_id - * The entity id. - - * - * @return \Drupal\Core\Entity\EntityInterface|null - * The active entity. - */ - private function getActiveEntity($entity_type_id, $entity_id) { - $entity = $this->entityTypeManager->getStorage($entity_type_id)->load($entity_id); - return $this->entityRepository->getActive($entity, $this->contextRepository->getAvailableContexts()); - } - } diff --git a/core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php b/core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php index f6d56f00ee..b1fddf13ef 100644 --- a/core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php +++ b/core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php @@ -4,11 +4,9 @@ use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityRepositoryInterface; -use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\FieldableEntityInterface; -use Drupal\Core\Entity\TranslatableInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage; use Drupal\layout_builder\SectionStorage\SectionStorageDefinition; @@ -128,19 +126,15 @@ public function testGetSectionListFromId($success, $expected_entity_type_id, $id $this->entityRepository->getTranslationFromContext(Argument::cetera())->shouldNotBeCalled(); if ($expected_entity_type_id) { - $entity_storage = $this->prophesize(EntityStorageInterface::class); - $entity_without_layout = $this->prophesize(FieldableEntityInterface::class); $entity_without_layout->hasField(OverridesSectionStorage::FIELD_NAME)->willReturn(FALSE); $entity_without_layout->get(OverridesSectionStorage::FIELD_NAME)->shouldNotBeCalled(); - $entity_storage->load('entity_without_layout')->willReturn($entity_without_layout->reveal()); + $this->entityRepository->getActive('my_entity_type', 'entity_without_layout')->willReturn($entity_without_layout->reveal()); $entity_with_layout = $this->prophesize(FieldableEntityInterface::class); $entity_with_layout->hasField(OverridesSectionStorage::FIELD_NAME)->willReturn(TRUE); $entity_with_layout->get(OverridesSectionStorage::FIELD_NAME)->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()); + $this->entityRepository->getActive('my_entity_type', 'entity_with_layout')->willReturn($entity_with_layout->reveal()); } else { $this->entityTypeManager->getStorage(Argument::any())->shouldNotBeCalled(); @@ -171,6 +165,16 @@ public function providerTestGetSectionListFromId() { 'my_entity_type', 'my_entity_type.entity_without_layout', ]; + $data['with value, with layout, fr'] = [ + TRUE, + 'my_entity_type', + 'my_entity_type.entity_with_layout.fr', + ]; + $data['with value, without layout, fr'] = [ + FALSE, + 'my_entity_type', + 'my_entity_type.entity_without_layout.fr', + ]; $data['empty value, empty defaults'] = [ FALSE, NULL, @@ -195,19 +199,16 @@ public function providerTestGetSectionListFromId() { */ 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(OverridesSectionStorage::FIELD_NAME)->willReturn(FALSE); - $entity_storage->load('entity_without_layout')->willReturn($entity_without_layout->reveal()); + $this->entityRepository->getActive($expected_entity_type_id, 'entity_without_layout')->willReturn($entity_without_layout->reveal()); $entity_with_layout = $this->prophesize(FieldableEntityInterface::class); $entity_with_layout->hasField(OverridesSectionStorage::FIELD_NAME)->willReturn(TRUE); - $entity_storage->load('entity_with_layout')->willReturn($entity_with_layout->reveal()); - $this->entityTypeManager->getStorage($expected_entity_type_id)->willReturn($entity_storage->reveal()); + $this->entityRepository->getActive($expected_entity_type_id, 'entity_with_layout')->willReturn($entity_with_layout->reveal()); } else { - $this->entityTypeManager->getStorage(Argument::any())->shouldNotBeCalled(); + $this->entityRepository->getActive(Argument::any())->shouldNotBeCalled(); } $method = new \ReflectionMethod($this->plugin, 'extractEntityFromRoute'); @@ -261,52 +262,6 @@ public function providerTestExtractEntityFromRoute() { return $data; } - /** - * @covers ::getSectionListFromId - */ - public function testGetSectionListFromIdTranslatableNoTranslation() { - $entity_storage = $this->prophesize(EntityStorageInterface::class); - - $entity_with_layout = $this->prophesize(FieldableEntityInterface::class)->willImplement(TranslatableInterface::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()); - - $translated_entity = $this->prophesize(FieldableEntityInterface::class)->willImplement(TranslatableInterface::class); - $translated_entity->hasField('layout_builder__layout')->willReturn(FALSE); - $translated_entity->get('layout_builder__layout')->shouldNotBeCalled(); - - $this->entityTypeManager->getStorage('my_entity_type')->willReturn($entity_storage->reveal()); - - $this->entityRepository->getTranslationFromContext($entity_with_layout->reveal(), 'fr')->willReturn($translated_entity->reveal()); - - $result = $this->plugin->getSectionListFromId('my_entity_type.entity_with_layout.fr'); - $this->assertEquals('the_return_value', $result); - } - - /** - * @covers ::getSectionListFromId - */ - public function testGetSectionListFromIdTranslatable() { - $entity_storage = $this->prophesize(EntityStorageInterface::class); - - $entity_with_layout = $this->prophesize(FieldableEntityInterface::class)->willImplement(TranslatableInterface::class); - $entity_with_layout->hasField('layout_builder__layout')->shouldNotBeCalled(); - $entity_with_layout->get('layout_builder__layout')->shouldNotBeCalled(); - $entity_storage->load('entity_with_layout')->willReturn($entity_with_layout->reveal()); - - $translated_entity = $this->prophesize(FieldableEntityInterface::class)->willImplement(TranslatableInterface::class); - $translated_entity->hasField('layout_builder__layout')->willReturn(TRUE); - $translated_entity->get('layout_builder__layout')->willReturn('the_return_value'); - - $this->entityTypeManager->getStorage('my_entity_type')->willReturn($entity_storage->reveal()); - - $this->entityRepository->getTranslationFromContext($entity_with_layout->reveal(), 'fr')->willReturn($translated_entity->reveal()); - - $result = $this->plugin->getSectionListFromId('my_entity_type.entity_with_layout.fr'); - $this->assertEquals('the_return_value', $result); - } - /** * @covers ::buildRoutes * @covers ::hasIntegerId