Same filename and directory in other branches
- 8.9.x core/modules/layout_builder/src/LayoutEntityHelperTrait.php
- 9 core/modules/layout_builder/src/LayoutEntityHelperTrait.php
Namespace
Drupal\layout_builder
File
core/modules/layout_builder/src/LayoutEntityHelperTrait.php
View source
<?php
namespace Drupal\layout_builder;
use Drupal\Component\Plugin\DerivativeInspectionInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\Context\EntityContext;
use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface;
trait LayoutEntityHelperTrait {
protected $sectionStorageManager;
protected function isLayoutCompatibleEntity(EntityInterface $entity) {
return $this
->getSectionStorageForEntity($entity) !== NULL;
}
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;
}
protected function getEntitySections(EntityInterface $entity) {
$section_storage = $this
->getSectionStorageForEntity($entity);
return $section_storage ? $section_storage
->getSections() : [];
}
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;
}
protected function getSectionStorageForEntity(EntityInterface $entity) {
$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());
}
protected function originalEntityUsesDefaultStorage(EntityInterface $entity) {
$section_storage = $this
->getSectionStorageForEntity($entity);
if ($section_storage instanceof OverridesSectionStorageInterface && !$entity
->isNew() && isset($entity->original)) {
$original_section_storage = $this
->getSectionStorageForEntity($entity->original);
return $original_section_storage instanceof DefaultsSectionStorageInterface;
}
return FALSE;
}
private function sectionStorageManager() {
return $this->sectionStorageManager ?: \Drupal::service('plugin.manager.layout_builder.section_storage');
}
}
Traits