Pull merge.
[yaffs-website] / web / core / modules / layout_builder / src / LayoutEntityHelperTrait.php
1 <?php
2
3 namespace Drupal\layout_builder;
4
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;
10
11 /**
12  * Methods to help with entities using the layout builder.
13  *
14  * @internal
15  */
16 trait LayoutEntityHelperTrait {
17
18   /**
19    * Determines if an entity can have a layout.
20    *
21    * @param \Drupal\Core\Entity\EntityInterface $entity
22    *   The entity to check.
23    *
24    * @return bool
25    *   TRUE if the entity can have a layout otherwise FALSE.
26    */
27   protected function isLayoutCompatibleEntity(EntityInterface $entity) {
28     return $entity instanceof LayoutEntityDisplayInterface || $this->isEntityUsingFieldOverride($entity);
29   }
30
31   /**
32    * Gets revision IDs for layout sections.
33    *
34    * @param \Drupal\layout_builder\Section[] $sections
35    *   The layout sections.
36    *
37    * @return int[]
38    *   The revision IDs.
39    */
40   protected function getInlineBlockRevisionIdsInSections(array $sections) {
41     $revision_ids = [];
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'];
46       }
47     }
48     return $revision_ids;
49   }
50
51   /**
52    * Gets the sections for an entity if any.
53    *
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.
57    *
58    * @param \Drupal\Core\Entity\EntityInterface $entity
59    *   The entity.
60    *
61    * @return \Drupal\layout_builder\Section[]|null
62    *   The entity layout sections if available.
63    */
64   protected function getEntitySections(EntityInterface $entity) {
65     if ($entity instanceof LayoutEntityDisplayInterface) {
66       return $entity->getSections();
67     }
68     elseif ($this->isEntityUsingFieldOverride($entity)) {
69       return $entity->get(OverridesSectionStorage::FIELD_NAME)->getSections();
70     }
71     return NULL;
72   }
73
74   /**
75    * Gets components that have Inline Block plugins.
76    *
77    * @param \Drupal\layout_builder\Section[] $sections
78    *   The layout sections.
79    *
80    * @return \Drupal\layout_builder\SectionComponent[]
81    *   The components that contain Inline Block plugins.
82    */
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;
90         }
91       }
92     }
93     return $inline_block_components;
94   }
95
96   /**
97    * Determines if an entity is using a field for the layout override.
98    *
99    * @param \Drupal\Core\Entity\EntityInterface $entity
100    *   The entity.
101    *
102    * @return bool
103    *   TRUE if the entity is using a field for a layout override.
104    */
105   protected function isEntityUsingFieldOverride(EntityInterface $entity) {
106     return $entity instanceof FieldableEntityInterface && $entity->hasField(OverridesSectionStorage::FIELD_NAME);
107   }
108
109 }