diff --git a/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php b/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php index b3292c4c86..1f4cc02d38 100644 --- a/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php +++ b/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php @@ -10,10 +10,10 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Entity\TranslatableInterface; -use Drupal\Core\Language\LanguageManagerInterface; 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; @@ -76,20 +76,22 @@ class OverridesSectionStorage extends SectionStorageBase implements ContainerFac protected $entityRepository; /** - * @var \Drupal\Core\Language\LanguageManagerInterface + * The context repository. + * + * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface */ - protected $languageManager; + protected $contextRepository; /** * {@inheritdoc} */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, EntityRepositoryInterface $entity_repository, LanguageManagerInterface $language_manager) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, EntityRepositoryInterface $entity_repository, ContextRepositoryInterface $context_repository) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->entityTypeManager = $entity_type_manager; $this->entityFieldManager = $entity_field_manager; $this->entityRepository = $entity_repository; - $this->languageManager = $language_manager; + $this->contextRepository = $context_repository; } /** @@ -103,7 +105,7 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('entity_type.manager'), $container->get('entity_field.manager'), $container->get('entity.repository'), - $container->get('language_manager') + $container->get('context.repository') ); } @@ -166,8 +168,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, $langcode) = $this->getIdPartsArray($id); - $entity = $this->getActiveEntity($entity_type_id, $entity_id, $langcode); + list($entity_type_id, $entity_id) = explode('.', $id, 2); + $entity = $this->getActiveEntity($entity_type_id, $entity_id); if ($entity instanceof FieldableEntityInterface && $entity->hasField(static::FIELD_NAME)) { return $entity->get(static::FIELD_NAME); } @@ -206,18 +208,17 @@ public function deriveContextsFromRoute($value, $definition, $name, array $defau */ private function extractEntityFromRoute($value, array $defaults) { if (strpos($value, '.') !== FALSE) { - list($entity_type_id, $entity_id, $langcode) = $this->getIdPartsArray($value); + list($entity_type_id, $entity_id) = explode('.', $value); } 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]; - $langcode = isset($defaults['langcode']) ? $defaults['langcode'] : $this->languageManager->getCurrentLanguage()->getId(); } else { return NULL; } - $entity = $this->getActiveEntity($entity_type_id, $entity_id, $langcode); + $entity = $this->getActiveEntity($entity_type_id, $entity_id); if ($entity instanceof FieldableEntityInterface && $entity->hasField(static::FIELD_NAME)) { return $entity; } @@ -382,46 +383,20 @@ public function isApplicable(RefinableCacheableDependencyInterface $cacheability } /** - * Gets the 3 parts of the storage ID. - * - * The 3 parts will be, entity_type, entity_id and langcode. langcode may be - * NULL if the ID does not contain it. - * - * @param string $id - * The ID. + * Gets the active entity for editing. * - * @return array - * The parts of ID. The 3 parts will be, entity_type, entity_id and - * langcode. langcode may be NULL if the ID does not contain it. - */ - protected function getIdPartsArray($id) { - $parts = explode('.', $id); - if (count($parts) === 2) { - $parts[2] = NULL; - } - return $parts; - } + * @param string $entity_type_id + * The entity type id. + * @param string $entity_id + * The entity id. - /** - * @param $entity_type_id - * @param $entity_id - * @param $langcode * * @return \Drupal\Core\Entity\EntityInterface|null - * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException - * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + * The active entity. */ - private function getActiveEntity($entity_type_id, $entity_id, $langcode) { - $entity = $this->entityTypeManager->getStorage($entity_type_id) - ->load($entity_id); - $contexts = []; - if ($langcode) { - $contexts[] = new Context(new ContextDefinition('language', 'Interface text'), $langcode); - $contexts[] = new Context(new ContextDefinition('language', 'Content'), $langcode); - } - - $entity = $this->entityRepository->getActive($entity, $contexts); - return $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()); } }