Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[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
10 /**
11  * Methods to help with entities using the layout builder.
12  *
13  * @internal
14  */
15 trait LayoutEntityHelperTrait {
16
17   /**
18    * Determines if an entity can have a layout.
19    *
20    * @param \Drupal\Core\Entity\EntityInterface $entity
21    *   The entity to check.
22    *
23    * @return bool
24    *   TRUE if the entity can have a layout otherwise FALSE.
25    */
26   protected function isLayoutCompatibleEntity(EntityInterface $entity) {
27     return $entity instanceof LayoutEntityDisplayInterface || $this->isEntityUsingFieldOverride($entity);
28   }
29
30   /**
31    * Gets revision IDs for layout sections.
32    *
33    * @param \Drupal\layout_builder\Section[] $sections
34    *   The layout sections.
35    *
36    * @return int[]
37    *   The revision IDs.
38    */
39   protected function getInlineBlockRevisionIdsInSections(array $sections) {
40     $revision_ids = [];
41     foreach ($this->getInlineBlockComponents($sections) as $component) {
42       $configuration = $component->getPlugin()->getConfiguration();
43       if (!empty($configuration['block_revision_id'])) {
44         $revision_ids[] = $configuration['block_revision_id'];
45       }
46     }
47     return $revision_ids;
48   }
49
50   /**
51    * Gets the sections for an entity if any.
52    *
53    * @todo Replace this method with calls to the SectionStorageManagerInterface
54    * method for getting sections from an entity in
55    * https://www.drupal.org/node/2986403.
56    *
57    * @param \Drupal\Core\Entity\EntityInterface $entity
58    *   The entity.
59    *
60    * @return \Drupal\layout_builder\Section[]|null
61    *   The entity layout sections if available.
62    */
63   protected function getEntitySections(EntityInterface $entity) {
64     if ($entity instanceof LayoutEntityDisplayInterface) {
65       return $entity->getSections();
66     }
67     elseif ($this->isEntityUsingFieldOverride($entity)) {
68       return $entity->get('layout_builder__layout')->getSections();
69     }
70     return NULL;
71   }
72
73   /**
74    * Gets components that have Inline Block plugins.
75    *
76    * @param \Drupal\layout_builder\Section[] $sections
77    *   The layout sections.
78    *
79    * @return \Drupal\layout_builder\SectionComponent[]
80    *   The components that contain Inline Block plugins.
81    */
82   protected function getInlineBlockComponents(array $sections) {
83     $inline_block_components = [];
84     foreach ($sections as $section) {
85       foreach ($section->getComponents() as $component) {
86         $plugin = $component->getPlugin();
87         if ($plugin instanceof DerivativeInspectionInterface && $plugin->getBaseId() === 'inline_block') {
88           $inline_block_components[] = $component;
89         }
90       }
91     }
92     return $inline_block_components;
93   }
94
95   /**
96    * Determines if an entity is using a field for the layout override.
97    *
98    * @param \Drupal\Core\Entity\EntityInterface $entity
99    *   The entity.
100    *
101    * @return bool
102    *   TRUE if the entity is using a field for a layout override.
103    */
104   protected function isEntityUsingFieldOverride(EntityInterface $entity) {
105     return $entity instanceof FieldableEntityInterface && $entity->hasField('layout_builder__layout');
106   }
107
108 }