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