comparison core/modules/layout_builder/src/LayoutEntityHelperTrait.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents 129ea1e6d783
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
1 <?php 1 <?php
2 2
3 namespace Drupal\layout_builder; 3 namespace Drupal\layout_builder;
4 4
5 use Drupal\Component\Plugin\DerivativeInspectionInterface; 5 use Drupal\Component\Plugin\DerivativeInspectionInterface;
6 use Drupal\Core\Cache\CacheableMetadata;
7 use Drupal\Core\Entity\Entity\EntityViewDisplay;
6 use Drupal\Core\Entity\EntityInterface; 8 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\FieldableEntityInterface; 9 use Drupal\Core\Entity\FieldableEntityInterface;
10 use Drupal\Core\Plugin\Context\Context;
11 use Drupal\Core\Plugin\Context\ContextDefinition;
12 use Drupal\Core\Plugin\Context\EntityContext;
8 use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface; 13 use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface;
9 use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage; 14 use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
10 15
11 /** 16 /**
12 * Methods to help with entities using the layout builder. 17 * Methods to help with entities using the layout builder.
13 *
14 * @internal
15 */ 18 */
16 trait LayoutEntityHelperTrait { 19 trait LayoutEntityHelperTrait {
20
21 /**
22 * The section storage manager.
23 *
24 * @var \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface
25 */
26 protected $sectionStorageManager;
17 27
18 /** 28 /**
19 * Determines if an entity can have a layout. 29 * Determines if an entity can have a layout.
20 * 30 *
21 * @param \Drupal\Core\Entity\EntityInterface $entity 31 * @param \Drupal\Core\Entity\EntityInterface $entity
23 * 33 *
24 * @return bool 34 * @return bool
25 * TRUE if the entity can have a layout otherwise FALSE. 35 * TRUE if the entity can have a layout otherwise FALSE.
26 */ 36 */
27 protected function isLayoutCompatibleEntity(EntityInterface $entity) { 37 protected function isLayoutCompatibleEntity(EntityInterface $entity) {
28 return $entity instanceof LayoutEntityDisplayInterface || $this->isEntityUsingFieldOverride($entity); 38 return $this->getSectionStorageForEntity($entity) !== NULL;
29 } 39 }
30 40
31 /** 41 /**
32 * Gets revision IDs for layout sections. 42 * Gets revision IDs for layout sections.
33 * 43 *
49 } 59 }
50 60
51 /** 61 /**
52 * Gets the sections for an entity if any. 62 * Gets the sections for an entity if any.
53 * 63 *
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 64 * @param \Drupal\Core\Entity\EntityInterface $entity
59 * The entity. 65 * The entity.
60 * 66 *
61 * @return \Drupal\layout_builder\Section[]|null 67 * @return \Drupal\layout_builder\Section[]
62 * The entity layout sections if available. 68 * The entity layout sections if available.
63 */ 69 */
64 protected function getEntitySections(EntityInterface $entity) { 70 protected function getEntitySections(EntityInterface $entity) {
65 if ($entity instanceof LayoutEntityDisplayInterface) { 71 $section_storage = $this->getSectionStorageForEntity($entity);
66 return $entity->getSections(); 72 return $section_storage ? $section_storage->getSections() : [];
67 }
68 elseif ($this->isEntityUsingFieldOverride($entity)) {
69 return $entity->get(OverridesSectionStorage::FIELD_NAME)->getSections();
70 }
71 return NULL;
72 } 73 }
73 74
74 /** 75 /**
75 * Gets components that have Inline Block plugins. 76 * Gets components that have Inline Block plugins.
76 * 77 *
92 } 93 }
93 return $inline_block_components; 94 return $inline_block_components;
94 } 95 }
95 96
96 /** 97 /**
98 * Gets the section storage for an entity.
99 *
100 * @param \Drupal\Core\Entity\EntityInterface $entity
101 * The entity.
102 *
103 * @return \Drupal\layout_builder\SectionStorageInterface|null
104 * The section storage if found otherwise NULL.
105 */
106 protected function getSectionStorageForEntity(EntityInterface $entity) {
107 // @todo Take into account other view modes in
108 // https://www.drupal.org/node/3008924.
109 $view_mode = 'full';
110 if ($entity instanceof LayoutEntityDisplayInterface) {
111 $contexts['display'] = EntityContext::fromEntity($entity);
112 }
113 else {
114 $contexts['entity'] = EntityContext::fromEntity($entity);
115 if ($entity instanceof FieldableEntityInterface) {
116 $display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
117 if ($display instanceof LayoutEntityDisplayInterface) {
118 $contexts['display'] = EntityContext::fromEntity($display);
119 }
120 $contexts['view_mode'] = new Context(new ContextDefinition('string'), $view_mode);
121 }
122 }
123 return $this->sectionStorageManager()->findByContext($contexts, new CacheableMetadata());
124 }
125
126 /**
97 * Determines if an entity is using a field for the layout override. 127 * Determines if an entity is using a field for the layout override.
98 * 128 *
99 * @param \Drupal\Core\Entity\EntityInterface $entity 129 * @param \Drupal\Core\Entity\EntityInterface $entity
100 * The entity. 130 * The entity.
101 * 131 *
102 * @return bool 132 * @return bool
103 * TRUE if the entity is using a field for a layout override. 133 * TRUE if the entity is using a field for a layout override.
134 *
135 * @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0.
136 * To determine if an entity has a layout override, use
137 * \Drupal\layout_builder\LayoutEntityHelperTrait::getSectionStorageForEntity()
138 * and check whether the result is an instance of
139 * \Drupal\layout_builder\DefaultsSectionStorageInterface.
140 *
141 * @see https://www.drupal.org/node/3030609
104 */ 142 */
105 protected function isEntityUsingFieldOverride(EntityInterface $entity) { 143 protected function isEntityUsingFieldOverride(EntityInterface $entity) {
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);
106 return $entity instanceof FieldableEntityInterface && $entity->hasField(OverridesSectionStorage::FIELD_NAME); 145 return $entity instanceof FieldableEntityInterface && $entity->hasField(OverridesSectionStorage::FIELD_NAME);
107 } 146 }
108 147
148 /**
149 * Determines if the original entity used the default section storage.
150 *
151 * This method can be used during the entity save process to determine whether
152 * $entity->original is set and used the default section storage plugin as
153 * determined by ::getSectionStorageForEntity().
154 *
155 * @param \Drupal\Core\Entity\EntityInterface $entity
156 * The entity.
157 *
158 * @return bool
159 * TRUE if the original entity used the default storage.
160 */
161 protected function originalEntityUsesDefaultStorage(EntityInterface $entity) {
162 $section_storage = $this->getSectionStorageForEntity($entity);
163 if ($section_storage instanceof OverridesSectionStorageInterface && !$entity->isNew() && isset($entity->original)) {
164 $original_section_storage = $this->getSectionStorageForEntity($entity->original);
165 return $original_section_storage instanceof DefaultsSectionStorageInterface;
166 }
167 return FALSE;
168 }
169
170 /**
171 * Gets the section storage manager.
172 *
173 * @return \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface
174 * The section storage manager.
175 */
176 private function sectionStorageManager() {
177 return $this->sectionStorageManager ?: \Drupal::service('plugin.manager.layout_builder.section_storage');
178 }
179
109 } 180 }