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@18
|
6 use Drupal\Core\Cache\CacheableMetadata;
|
Chris@18
|
7 use Drupal\Core\Entity\Entity\EntityViewDisplay;
|
Chris@17
|
8 use Drupal\Core\Entity\EntityInterface;
|
Chris@17
|
9 use Drupal\Core\Entity\FieldableEntityInterface;
|
Chris@18
|
10 use Drupal\Core\Plugin\Context\Context;
|
Chris@18
|
11 use Drupal\Core\Plugin\Context\ContextDefinition;
|
Chris@18
|
12 use Drupal\Core\Plugin\Context\EntityContext;
|
Chris@17
|
13 use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface;
|
Chris@17
|
14 use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
|
Chris@17
|
15
|
Chris@17
|
16 /**
|
Chris@17
|
17 * Methods to help with entities using the layout builder.
|
Chris@17
|
18 */
|
Chris@17
|
19 trait LayoutEntityHelperTrait {
|
Chris@17
|
20
|
Chris@17
|
21 /**
|
Chris@18
|
22 * The section storage manager.
|
Chris@18
|
23 *
|
Chris@18
|
24 * @var \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface
|
Chris@18
|
25 */
|
Chris@18
|
26 protected $sectionStorageManager;
|
Chris@18
|
27
|
Chris@18
|
28 /**
|
Chris@17
|
29 * Determines if an entity can have a layout.
|
Chris@17
|
30 *
|
Chris@17
|
31 * @param \Drupal\Core\Entity\EntityInterface $entity
|
Chris@17
|
32 * The entity to check.
|
Chris@17
|
33 *
|
Chris@17
|
34 * @return bool
|
Chris@17
|
35 * TRUE if the entity can have a layout otherwise FALSE.
|
Chris@17
|
36 */
|
Chris@17
|
37 protected function isLayoutCompatibleEntity(EntityInterface $entity) {
|
Chris@18
|
38 return $this->getSectionStorageForEntity($entity) !== NULL;
|
Chris@17
|
39 }
|
Chris@17
|
40
|
Chris@17
|
41 /**
|
Chris@17
|
42 * Gets revision IDs for layout sections.
|
Chris@17
|
43 *
|
Chris@17
|
44 * @param \Drupal\layout_builder\Section[] $sections
|
Chris@17
|
45 * The layout sections.
|
Chris@17
|
46 *
|
Chris@17
|
47 * @return int[]
|
Chris@17
|
48 * The revision IDs.
|
Chris@17
|
49 */
|
Chris@17
|
50 protected function getInlineBlockRevisionIdsInSections(array $sections) {
|
Chris@17
|
51 $revision_ids = [];
|
Chris@17
|
52 foreach ($this->getInlineBlockComponents($sections) as $component) {
|
Chris@17
|
53 $configuration = $component->getPlugin()->getConfiguration();
|
Chris@17
|
54 if (!empty($configuration['block_revision_id'])) {
|
Chris@17
|
55 $revision_ids[] = $configuration['block_revision_id'];
|
Chris@17
|
56 }
|
Chris@17
|
57 }
|
Chris@17
|
58 return $revision_ids;
|
Chris@17
|
59 }
|
Chris@17
|
60
|
Chris@17
|
61 /**
|
Chris@17
|
62 * Gets the sections for an entity if any.
|
Chris@17
|
63 *
|
Chris@17
|
64 * @param \Drupal\Core\Entity\EntityInterface $entity
|
Chris@17
|
65 * The entity.
|
Chris@17
|
66 *
|
Chris@18
|
67 * @return \Drupal\layout_builder\Section[]
|
Chris@17
|
68 * The entity layout sections if available.
|
Chris@17
|
69 */
|
Chris@17
|
70 protected function getEntitySections(EntityInterface $entity) {
|
Chris@18
|
71 $section_storage = $this->getSectionStorageForEntity($entity);
|
Chris@18
|
72 return $section_storage ? $section_storage->getSections() : [];
|
Chris@17
|
73 }
|
Chris@17
|
74
|
Chris@17
|
75 /**
|
Chris@17
|
76 * Gets components that have Inline Block plugins.
|
Chris@17
|
77 *
|
Chris@17
|
78 * @param \Drupal\layout_builder\Section[] $sections
|
Chris@17
|
79 * The layout sections.
|
Chris@17
|
80 *
|
Chris@17
|
81 * @return \Drupal\layout_builder\SectionComponent[]
|
Chris@17
|
82 * The components that contain Inline Block plugins.
|
Chris@17
|
83 */
|
Chris@17
|
84 protected function getInlineBlockComponents(array $sections) {
|
Chris@17
|
85 $inline_block_components = [];
|
Chris@17
|
86 foreach ($sections as $section) {
|
Chris@17
|
87 foreach ($section->getComponents() as $component) {
|
Chris@17
|
88 $plugin = $component->getPlugin();
|
Chris@17
|
89 if ($plugin instanceof DerivativeInspectionInterface && $plugin->getBaseId() === 'inline_block') {
|
Chris@17
|
90 $inline_block_components[] = $component;
|
Chris@17
|
91 }
|
Chris@17
|
92 }
|
Chris@17
|
93 }
|
Chris@17
|
94 return $inline_block_components;
|
Chris@17
|
95 }
|
Chris@17
|
96
|
Chris@17
|
97 /**
|
Chris@18
|
98 * Gets the section storage for an entity.
|
Chris@18
|
99 *
|
Chris@18
|
100 * @param \Drupal\Core\Entity\EntityInterface $entity
|
Chris@18
|
101 * The entity.
|
Chris@18
|
102 *
|
Chris@18
|
103 * @return \Drupal\layout_builder\SectionStorageInterface|null
|
Chris@18
|
104 * The section storage if found otherwise NULL.
|
Chris@18
|
105 */
|
Chris@18
|
106 protected function getSectionStorageForEntity(EntityInterface $entity) {
|
Chris@18
|
107 // @todo Take into account other view modes in
|
Chris@18
|
108 // https://www.drupal.org/node/3008924.
|
Chris@18
|
109 $view_mode = 'full';
|
Chris@18
|
110 if ($entity instanceof LayoutEntityDisplayInterface) {
|
Chris@18
|
111 $contexts['display'] = EntityContext::fromEntity($entity);
|
Chris@18
|
112 }
|
Chris@18
|
113 else {
|
Chris@18
|
114 $contexts['entity'] = EntityContext::fromEntity($entity);
|
Chris@18
|
115 if ($entity instanceof FieldableEntityInterface) {
|
Chris@18
|
116 $display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
|
Chris@18
|
117 if ($display instanceof LayoutEntityDisplayInterface) {
|
Chris@18
|
118 $contexts['display'] = EntityContext::fromEntity($display);
|
Chris@18
|
119 }
|
Chris@18
|
120 $contexts['view_mode'] = new Context(new ContextDefinition('string'), $view_mode);
|
Chris@18
|
121 }
|
Chris@18
|
122 }
|
Chris@18
|
123 return $this->sectionStorageManager()->findByContext($contexts, new CacheableMetadata());
|
Chris@18
|
124 }
|
Chris@18
|
125
|
Chris@18
|
126 /**
|
Chris@17
|
127 * Determines if an entity is using a field for the layout override.
|
Chris@17
|
128 *
|
Chris@17
|
129 * @param \Drupal\Core\Entity\EntityInterface $entity
|
Chris@17
|
130 * The entity.
|
Chris@17
|
131 *
|
Chris@17
|
132 * @return bool
|
Chris@17
|
133 * TRUE if the entity is using a field for a layout override.
|
Chris@18
|
134 *
|
Chris@18
|
135 * @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0.
|
Chris@18
|
136 * To determine if an entity has a layout override, use
|
Chris@18
|
137 * \Drupal\layout_builder\LayoutEntityHelperTrait::getSectionStorageForEntity()
|
Chris@18
|
138 * and check whether the result is an instance of
|
Chris@18
|
139 * \Drupal\layout_builder\DefaultsSectionStorageInterface.
|
Chris@18
|
140 *
|
Chris@18
|
141 * @see https://www.drupal.org/node/3030609
|
Chris@17
|
142 */
|
Chris@17
|
143 protected function isEntityUsingFieldOverride(EntityInterface $entity) {
|
Chris@18
|
144 @trigger_error('\Drupal\layout_builder\LayoutEntityHelperTrait::isEntityUsingFieldOverride() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Internal storage of overrides may change so the existence of the field does not necessarily guarantee an overridable entity. See https://www.drupal.org/node/3030609.', E_USER_DEPRECATED);
|
Chris@17
|
145 return $entity instanceof FieldableEntityInterface && $entity->hasField(OverridesSectionStorage::FIELD_NAME);
|
Chris@17
|
146 }
|
Chris@17
|
147
|
Chris@18
|
148 /**
|
Chris@18
|
149 * Determines if the original entity used the default section storage.
|
Chris@18
|
150 *
|
Chris@18
|
151 * This method can be used during the entity save process to determine whether
|
Chris@18
|
152 * $entity->original is set and used the default section storage plugin as
|
Chris@18
|
153 * determined by ::getSectionStorageForEntity().
|
Chris@18
|
154 *
|
Chris@18
|
155 * @param \Drupal\Core\Entity\EntityInterface $entity
|
Chris@18
|
156 * The entity.
|
Chris@18
|
157 *
|
Chris@18
|
158 * @return bool
|
Chris@18
|
159 * TRUE if the original entity used the default storage.
|
Chris@18
|
160 */
|
Chris@18
|
161 protected function originalEntityUsesDefaultStorage(EntityInterface $entity) {
|
Chris@18
|
162 $section_storage = $this->getSectionStorageForEntity($entity);
|
Chris@18
|
163 if ($section_storage instanceof OverridesSectionStorageInterface && !$entity->isNew() && isset($entity->original)) {
|
Chris@18
|
164 $original_section_storage = $this->getSectionStorageForEntity($entity->original);
|
Chris@18
|
165 return $original_section_storage instanceof DefaultsSectionStorageInterface;
|
Chris@18
|
166 }
|
Chris@18
|
167 return FALSE;
|
Chris@18
|
168 }
|
Chris@18
|
169
|
Chris@18
|
170 /**
|
Chris@18
|
171 * Gets the section storage manager.
|
Chris@18
|
172 *
|
Chris@18
|
173 * @return \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface
|
Chris@18
|
174 * The section storage manager.
|
Chris@18
|
175 */
|
Chris@18
|
176 private function sectionStorageManager() {
|
Chris@18
|
177 return $this->sectionStorageManager ?: \Drupal::service('plugin.manager.layout_builder.section_storage');
|
Chris@18
|
178 }
|
Chris@18
|
179
|
Chris@17
|
180 }
|