3 namespace Drupal\layout_builder;
5 use Drupal\Component\Plugin\DerivativeInspectionInterface;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\FieldableEntityInterface;
8 use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface;
9 use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
12 * Methods to help with entities using the layout builder.
16 trait LayoutEntityHelperTrait {
19 * Determines if an entity can have a layout.
21 * @param \Drupal\Core\Entity\EntityInterface $entity
22 * The entity to check.
25 * TRUE if the entity can have a layout otherwise FALSE.
27 protected function isLayoutCompatibleEntity(EntityInterface $entity) {
28 return $entity instanceof LayoutEntityDisplayInterface || $this->isEntityUsingFieldOverride($entity);
32 * Gets revision IDs for layout sections.
34 * @param \Drupal\layout_builder\Section[] $sections
35 * The layout sections.
40 protected function getInlineBlockRevisionIdsInSections(array $sections) {
42 foreach ($this->getInlineBlockComponents($sections) as $component) {
43 $configuration = $component->getPlugin()->getConfiguration();
44 if (!empty($configuration['block_revision_id'])) {
45 $revision_ids[] = $configuration['block_revision_id'];
52 * Gets the sections for an entity if any.
54 * @todo Replace this method with calls to the SectionStorageManagerInterface
55 * method for getting sections from an entity in
56 * https://www.drupal.org/node/2986403.
58 * @param \Drupal\Core\Entity\EntityInterface $entity
61 * @return \Drupal\layout_builder\Section[]|null
62 * The entity layout sections if available.
64 protected function getEntitySections(EntityInterface $entity) {
65 if ($entity instanceof LayoutEntityDisplayInterface) {
66 return $entity->getSections();
68 elseif ($this->isEntityUsingFieldOverride($entity)) {
69 return $entity->get(OverridesSectionStorage::FIELD_NAME)->getSections();
75 * Gets components that have Inline Block plugins.
77 * @param \Drupal\layout_builder\Section[] $sections
78 * The layout sections.
80 * @return \Drupal\layout_builder\SectionComponent[]
81 * The components that contain Inline Block plugins.
83 protected function getInlineBlockComponents(array $sections) {
84 $inline_block_components = [];
85 foreach ($sections as $section) {
86 foreach ($section->getComponents() as $component) {
87 $plugin = $component->getPlugin();
88 if ($plugin instanceof DerivativeInspectionInterface && $plugin->getBaseId() === 'inline_block') {
89 $inline_block_components[] = $component;
93 return $inline_block_components;
97 * Determines if an entity is using a field for the layout override.
99 * @param \Drupal\Core\Entity\EntityInterface $entity
103 * TRUE if the entity is using a field for a layout override.
105 protected function isEntityUsingFieldOverride(EntityInterface $entity) {
106 return $entity instanceof FieldableEntityInterface && $entity->hasField(OverridesSectionStorage::FIELD_NAME);