-
-
Save timplunkett/0443bdd20d1d80ed2dbb8ce9b86ed761 to your computer and use it in GitHub Desktop.
Potential change to entity rendering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/core/modules/layout_builder/layout_builder.module b/core/modules/layout_builder/layout_builder.module | |
index b1ecb5d594..a832cbcc22 100644 | |
--- a/core/modules/layout_builder/layout_builder.module | |
+++ b/core/modules/layout_builder/layout_builder.module | |
@@ -7,11 +7,15 @@ | |
use Drupal\Core\Entity\FieldableEntityInterface; | |
use Drupal\Core\Form\FormStateInterface; | |
+use Drupal\Core\Plugin\Context\Context; | |
+use Drupal\Core\Plugin\Context\ContextDefinition; | |
use Drupal\Core\Routing\RouteMatchInterface; | |
+use Drupal\Core\StringTranslation\TranslatableMarkup; | |
use Drupal\Core\Url; | |
use Drupal\field\FieldConfigInterface; | |
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay; | |
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplayStorage; | |
+use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface; | |
use Drupal\layout_builder\Form\LayoutBuilderEntityViewDisplayForm; | |
/** | |
@@ -87,3 +91,48 @@ function layout_builder_field_config_delete(FieldConfigInterface $field_config) | |
$sample_entity_generator->delete($field_config->getTargetEntityTypeId(), $field_config->getTargetBundle()); | |
\Drupal::service('plugin.manager.block')->clearCachedDefinitions(); | |
} | |
+ | |
+/** | |
+ * Implements hook_entity_view_alter(). | |
+ */ | |
+function layout_builder_entity_view_alter(array &$build, FieldableEntityInterface $entity, LayoutEntityDisplayInterface $display) { | |
+ $sections = _layout_builder_get_runtime_sections($entity, $display); | |
+ if ($sections) { | |
+ /** @var \Drupal\Core\Field\FieldDefinitionInterface[] $field_definitions */ | |
+ $field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($display->getTargetEntityTypeId(), $display->getTargetBundle()); | |
+ foreach ($build as $name => $build_part) { | |
+ if (isset($field_definitions[$name]) && $field_definitions[$name]->isDisplayConfigurable('view')) { | |
+ unset($build[$name]); | |
+ } | |
+ } | |
+ | |
+ // Bypass ::getContexts() in order to use the runtime entity, not a | |
+ // sample entity. | |
+ $contexts = \Drupal::service('context.repository')->getAvailableContexts(); | |
+ // @todo Use EntityContextDefinition after resolving | |
+ // https://www.drupal.org/node/2932462. | |
+ $contexts['layout_builder.entity'] = new Context(new ContextDefinition("entity:{$entity->getEntityTypeId()}", new TranslatableMarkup('@entity being viewed', ['@entity' => $entity->getEntityType()->getLabel()])), $entity); | |
+ foreach ($sections as $delta => $section) { | |
+ $build['_layout_builder'][$delta] = $section->toRenderArray($contexts); | |
+ } | |
+ } | |
+} | |
+ | |
+/** | |
+ * Gets the runtime sections for a given entity. | |
+ * | |
+ * @param \Drupal\Core\Entity\FieldableEntityInterface $entity | |
+ * The entity. | |
+ * @param \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface $display | |
+ * The entity display. | |
+ * | |
+ * @return \Drupal\layout_builder\Section[] | |
+ * The sections. | |
+ */ | |
+function _layout_builder_get_runtime_sections(FieldableEntityInterface $entity, LayoutEntityDisplayInterface $display) { | |
+ if ($display->isOverridable() && !$entity->get('layout_builder__layout')->isEmpty()) { | |
+ return $entity->get('layout_builder__layout')->getSections(); | |
+ } | |
+ | |
+ return $display->getSections(); | |
+} | |
diff --git a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php | |
index 2582d5c7ed..e18d650e7a 100644 | |
--- a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php | |
+++ b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php | |
@@ -7,9 +7,6 @@ | |
use Drupal\Component\Utility\NestedArray; | |
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\StringTranslation\TranslatableMarkup; | |
use Drupal\field\Entity\FieldConfig; | |
use Drupal\field\Entity\FieldStorageConfig; | |
@@ -134,54 +131,6 @@ protected function contextRepository() { | |
return \Drupal::service('context.repository'); | |
} | |
- /** | |
- * {@inheritdoc} | |
- */ | |
- public function buildMultiple(array $entities) { | |
- $build_list = parent::buildMultiple($entities); | |
- | |
- foreach ($entities as $id => $entity) { | |
- $sections = $this->getRuntimeSections($entity); | |
- if ($sections) { | |
- foreach ($build_list[$id] as $name => $build_part) { | |
- $field_definition = $this->getFieldDefinition($name); | |
- if ($field_definition && $field_definition->isDisplayConfigurable($this->displayContext)) { | |
- unset($build_list[$id][$name]); | |
- } | |
- } | |
- | |
- // Bypass ::getContexts() in order to use the runtime entity, not a | |
- // sample entity. | |
- $contexts = $this->contextRepository()->getAvailableContexts(); | |
- // @todo Use EntityContextDefinition after resolving | |
- // https://www.drupal.org/node/2932462. | |
- $contexts['layout_builder.entity'] = new Context(new ContextDefinition("entity:{$entity->getEntityTypeId()}", new TranslatableMarkup('@entity being viewed', ['@entity' => $entity->getEntityType()->getLabel()])), $entity); | |
- foreach ($sections as $delta => $section) { | |
- $build_list[$id]['_layout_builder'][$delta] = $section->toRenderArray($contexts); | |
- } | |
- } | |
- } | |
- | |
- return $build_list; | |
- } | |
- | |
- /** | |
- * Gets the runtime sections for a given entity. | |
- * | |
- * @param \Drupal\Core\Entity\FieldableEntityInterface $entity | |
- * The entity. | |
- * | |
- * @return \Drupal\layout_builder\Section[] | |
- * The sections. | |
- */ | |
- protected function getRuntimeSections(FieldableEntityInterface $entity) { | |
- if ($this->isOverridable() && !$entity->get('layout_builder__layout')->isEmpty()) { | |
- return $entity->get('layout_builder__layout')->getSections(); | |
- } | |
- | |
- return $this->getSections(); | |
- } | |
- | |
/** | |
* {@inheritdoc} | |
* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment