trait LayoutEntityHelperTrait

Methods to help with entities using the layout builder.

Hierarchy

5 files declare their use of LayoutEntityHelperTrait
LayoutBuilderEntityViewDisplay.php in core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php
LayoutEntityHelperTraitTest.php in core/modules/layout_builder/tests/src/Unit/LayoutEntityHelperTraitTest.php
LayoutEntityHelperTraitTest.php in core/modules/layout_builder/tests/src/Kernel/LayoutEntityHelperTraitTest.php
OverridesSectionStorage.php in core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php
SetInlineBlockDependency.php in core/modules/layout_builder/src/EventSubscriber/SetInlineBlockDependency.php

File

core/modules/layout_builder/src/LayoutEntityHelperTrait.php, line 20

Namespace

Drupal\layout_builder
View source
trait LayoutEntityHelperTrait {
  
  /**
   * The section storage manager.
   */
  protected SectionStorageManagerInterface $sectionStorageManager;
  
  /**
   * Determines if an entity can have a layout.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity to check.
   *
   * @return bool
   *   TRUE if the entity can have a layout otherwise FALSE.
   */
  protected function isLayoutCompatibleEntity(EntityInterface $entity) {
    return $this->getSectionStorageForEntity($entity) !== NULL;
  }
  
  /**
   * Gets revision IDs for layout sections.
   *
   * @param \Drupal\layout_builder\Section[] $sections
   *   The layout sections.
   *
   * @return int[]
   *   The revision IDs.
   */
  protected function getInlineBlockRevisionIdsInSections(array $sections) {
    $revision_ids = [];
    foreach ($this->getInlineBlockComponents($sections) as $component) {
      $configuration = $component->getPlugin()
        ->getConfiguration();
      if (!empty($configuration['block_revision_id'])) {
        $revision_ids[] = $configuration['block_revision_id'];
      }
    }
    return $revision_ids;
  }
  
  /**
   * Gets the sections for an entity if any.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity.
   *
   * @return \Drupal\layout_builder\Section[]
   *   The entity layout sections if available.
   */
  protected function getEntitySections(EntityInterface $entity) {
    $section_storage = $this->getSectionStorageForEntity($entity);
    return $section_storage ? $section_storage->getSections() : [];
  }
  
  /**
   * Gets components that have Inline Block plugins.
   *
   * @param \Drupal\layout_builder\Section[] $sections
   *   The layout sections.
   *
   * @return \Drupal\layout_builder\SectionComponent[]
   *   The components that contain Inline Block plugins.
   */
  protected function getInlineBlockComponents(array $sections) {
    $inline_block_components = [];
    foreach ($sections as $section) {
      foreach ($section->getComponents() as $component) {
        $plugin = $component->getPlugin();
        if ($plugin instanceof DerivativeInspectionInterface && $plugin->getBaseId() === 'inline_block') {
          $inline_block_components[] = $component;
        }
      }
    }
    return $inline_block_components;
  }
  
  /**
   * Gets the section storage for an entity.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity.
   *
   * @return \Drupal\layout_builder\SectionStorageInterface|null
   *   The section storage if found otherwise NULL.
   */
  protected function getSectionStorageForEntity(EntityInterface $entity) {
    // @todo Take into account other view modes in
    //   https://www.drupal.org/node/3008924.
    $view_mode = 'full';
    if ($entity instanceof LayoutEntityDisplayInterface) {
      $contexts['display'] = EntityContext::fromEntity($entity);
      $contexts['view_mode'] = new Context(new ContextDefinition('string'), $entity->getMode());
    }
    else {
      $contexts['entity'] = EntityContext::fromEntity($entity);
      if ($entity instanceof FieldableEntityInterface) {
        $display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
        if ($display instanceof LayoutEntityDisplayInterface) {
          $contexts['display'] = EntityContext::fromEntity($display);
        }
        $contexts['view_mode'] = new Context(new ContextDefinition('string'), $view_mode);
      }
    }
    return $this->sectionStorageManager()
      ->findByContext($contexts, new CacheableMetadata());
  }
  
  /**
   * Determines if the original entity used the default section storage.
   *
   * This method can be used during the entity save process to determine whether
   * the original entity is set and used the default section storage plugin as
   * determined by ::getSectionStorageForEntity().
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity.
   *
   * @return bool
   *   TRUE if the original entity used the default storage.
   */
  protected function originalEntityUsesDefaultStorage(EntityInterface $entity) {
    $section_storage = $this->getSectionStorageForEntity($entity);
    if ($section_storage instanceof OverridesSectionStorageInterface && !$entity->isNew() && $entity->getOriginal()) {
      $original_section_storage = $this->getSectionStorageForEntity($entity->getOriginal());
      return $original_section_storage instanceof DefaultsSectionStorageInterface;
    }
    return FALSE;
  }
  
  /**
   * Gets the section storage manager.
   *
   * @return \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface
   *   The section storage manager.
   */
  private function sectionStorageManager() {
    return $this->sectionStorageManager ?? \Drupal::service('plugin.manager.layout_builder.section_storage');
  }
  
  /**
   * Gets the overrides section storage for an entity.
   *
   * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
   *   The entity with the overridden layout.
   *
   * @return \Drupal\layout_builder\OverridesSectionStorageInterface|null
   *   The overrides section storage if found otherwise NULL.
   */
  public function getOverridesSectionStorageForEntity(FieldableEntityInterface $entity) : ?OverridesSectionStorageInterface {
    $contexts = $this->getSectionStorageContextsForEntity($entity);
    return $this->sectionStorageManager()
      ->load('overrides', $contexts);
  }
  
  /**
   * Gets the section storage contexts for an entity.
   *
   * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
   *   The entity with the overridden layout.
   *
   * @return \Drupal\Core\Plugin\Context\ContextInterface[]
   *   The section storage contexts.
   */
  public function getSectionStorageContextsForEntity(FieldableEntityInterface $entity) : array {
    $contexts = [];
    $contexts['entity'] = EntityContext::fromEntity($entity);
    // @todo Expand to work for all view modes in
    //   https://www.drupal.org/node/2907413.
    $view_mode = 'full';
    // Retrieve the actual view mode from the returned view display as the
    // requested view mode may not exist and a fallback will be used.
    $view_mode = LayoutBuilderEntityViewDisplay::collectRenderDisplay($entity, $view_mode)->getMode();
    $contexts['view_mode'] = new Context(new ContextDefinition('string'), $view_mode);
    return $contexts;
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
LayoutEntityHelperTrait::$sectionStorageManager protected property The section storage manager. 1
LayoutEntityHelperTrait::getEntitySections protected function Gets the sections for an entity if any.
LayoutEntityHelperTrait::getInlineBlockComponents protected function Gets components that have Inline Block plugins.
LayoutEntityHelperTrait::getInlineBlockRevisionIdsInSections protected function Gets revision IDs for layout sections.
LayoutEntityHelperTrait::getOverridesSectionStorageForEntity public function Gets the overrides section storage for an entity.
LayoutEntityHelperTrait::getSectionStorageContextsForEntity public function Gets the section storage contexts for an entity.
LayoutEntityHelperTrait::getSectionStorageForEntity protected function Gets the section storage for an entity.
LayoutEntityHelperTrait::isLayoutCompatibleEntity protected function Determines if an entity can have a layout.
LayoutEntityHelperTrait::originalEntityUsesDefaultStorage protected function Determines if the original entity used the default section storage.
LayoutEntityHelperTrait::sectionStorageManager private function Gets the section storage manager. 1

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.