diff --git a/core/modules/layout_builder/layout_builder.module b/core/modules/layout_builder/layout_builder.module index 6dddc2d9e0..1d5ea91479 100644 --- a/core/modules/layout_builder/layout_builder.module +++ b/core/modules/layout_builder/layout_builder.module @@ -170,7 +170,9 @@ function layout_builder_add_layout_section_field($entity_type_id, $bundle, $fiel function layout_builder_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) { if ($display->getThirdPartySetting('layout_builder', 'allow_custom', FALSE) && !$entity->layout_builder__layout->isEmpty()) { $contexts = \Drupal::service('context.repository')->getAvailableContexts(); - $contexts += _layout_builder_get_contexts_for_entity($entity); + // @todo Use EntityContextDefinition after resolving + // https://www.drupal.org/node/2932462. + $contexts['layout_builder.entity'] = new Context(new ContextDefinition("entity:{$entity->getEntityTypeId()}", new TranslatableMarkup('Current @entity', ['@entity' => $entity->getEntityType()->getSingularLabel()])), $entity); $sections = $entity->layout_builder__layout->getSections(); foreach ($sections as $delta => $section) { $build['_layout_builder'][$delta] = $section->toRenderArray($contexts); @@ -192,22 +194,3 @@ function layout_builder_entity_view_alter(array &$build, EntityInterface $entity } } } - -/** - * Produces contexts for the entity. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity being laid out. - * - * @return \Drupal\Core\Plugin\Context\ContextInterface[] - * The array of context objects. - * - * @todo Use EntityContextDefinition after resolving - * https://www.drupal.org/node/2932462. - */ -function _layout_builder_get_contexts_for_entity(EntityInterface $entity) { - $definition = new ContextDefinition("entity:{$entity->getEntityTypeId()}", new TranslatableMarkup('Current @entity', [ - '@entity' => $entity->getEntityType()->getSingularLabel(), - ])); - return ['layout_builder.entity' => new Context($definition, $entity)]; -} diff --git a/core/modules/layout_builder/src/Field/LayoutSectionItemList.php b/core/modules/layout_builder/src/Field/LayoutSectionItemList.php index 6fcf42da0b..68de5bde27 100644 --- a/core/modules/layout_builder/src/Field/LayoutSectionItemList.php +++ b/core/modules/layout_builder/src/Field/LayoutSectionItemList.php @@ -3,6 +3,9 @@ namespace Drupal\layout_builder\Field; use Drupal\Core\Field\FieldItemList; +use Drupal\Core\Plugin\Context\Context; +use Drupal\Core\Plugin\Context\ContextDefinition; +use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\layout_builder\Section; use Drupal\layout_builder\SectionStorageInterface; @@ -78,7 +81,11 @@ public function removeSection($delta) { * {@inheritdoc} */ public function getContexts() { - return _layout_builder_get_contexts_for_entity($this->getEntity()); + $entity = $this->getEntity(); + // @todo Use EntityContextDefinition after resolving + // https://www.drupal.org/node/2932462. + $contexts['layout_builder.entity'] = new Context(new ContextDefinition("entity:{$entity->getEntityTypeId()}", new TranslatableMarkup('Current @entity', ['@entity' => $entity->getEntityType()->getSingularLabel()])), $entity); + return $contexts; } /**