1 | <?php |
||
25 | trait EntityHelperTrait { |
||
26 | |||
27 | /** |
||
28 | * The entity manager service. |
||
29 | * |
||
30 | * @var \Drupal\Core\Entity\EntityManagerInterface |
||
31 | */ |
||
32 | protected $entityManager; |
||
33 | |||
34 | /** |
||
35 | * The module handler service. |
||
36 | * |
||
37 | * @var \Drupal\Core\Extension\ModuleHandlerInterface. |
||
38 | */ |
||
39 | protected $moduleHandler; |
||
40 | |||
41 | /** |
||
42 | * The Entity Embed Display plugin manager. |
||
43 | * |
||
44 | * @var \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager. |
||
45 | */ |
||
46 | protected $displayPluginManager; |
||
47 | |||
48 | /** |
||
49 | * The renderer. |
||
50 | * |
||
51 | * @var \Drupal\Core\Render\RendererInterface. |
||
52 | */ |
||
53 | protected $renderer; |
||
54 | |||
55 | /** |
||
56 | * Loads an entity from the database. |
||
57 | * |
||
58 | * @param string $entity_type |
||
59 | * The entity type to load, e.g. node or user. |
||
60 | * @param mixed $id |
||
61 | * The id or UUID of the entity to load. |
||
62 | * |
||
63 | * @return \Drupal\Core\Entity\EntityInterface |
||
64 | * The entity object, or NULL if there is no entity with the given id or |
||
65 | * UUID. |
||
66 | */ |
||
67 | protected function loadEntity($entity_type, $id) { |
||
71 | |||
72 | /** |
||
73 | * Loads multiple entities from the database. |
||
74 | * |
||
75 | * @param string $entity_type |
||
76 | * The entity type to load, e.g. node or user. |
||
77 | * @param array $ids |
||
78 | * An array of entity IDs or UUIDs. |
||
79 | * |
||
80 | * @return array |
||
81 | * An array of entity objects indexed by their ids. |
||
82 | * |
||
83 | * @throws \Drupal\Core\Entity\EntityStorageException |
||
84 | * Throws an exception if the entity type does not supports UUIDs. |
||
85 | */ |
||
86 | protected function loadMultipleEntities($entity_type, array $ids) { |
||
105 | |||
106 | /** |
||
107 | * Determines if an entity can be rendered. |
||
108 | * |
||
109 | * @param \Drupal\Core\Entity\EntityInterface $entity |
||
110 | * The entity object. |
||
111 | * |
||
112 | * @return bool |
||
113 | * TRUE if the entity's type has a view builder controller, otherwise FALSE. |
||
114 | */ |
||
115 | protected function canRenderEntity(EntityInterface $entity) { |
||
119 | |||
120 | /** |
||
121 | * Determines if an entity type can be rendered. |
||
122 | * |
||
123 | * @param string $entity_type |
||
124 | * The entity type id. |
||
125 | * |
||
126 | * @return bool |
||
127 | * TRUE if the entitys type has a view builder controller, otherwise FALSE. |
||
128 | */ |
||
129 | protected function canRenderEntityType($entity_type) { |
||
132 | |||
133 | /** |
||
134 | * Returns the render array for an entity. |
||
135 | * |
||
136 | * @param \Drupal\Core\Entity\EntityInterface $entity |
||
137 | * The entity to be rendered. |
||
138 | * @param string $view_mode |
||
139 | * The view mode that should be used to display the entity. |
||
140 | * @param string $langcode |
||
141 | * (optional) For which language the entity should be rendered, defaults to |
||
142 | * the current content language. |
||
143 | * |
||
144 | * @return array |
||
145 | * A render array for the entity. |
||
146 | */ |
||
147 | protected function renderEntity(EntityInterface $entity, $view_mode, $langcode = NULL) { |
||
151 | |||
152 | /** |
||
153 | * Renders an embedded entity. |
||
154 | * |
||
155 | * @param \Drupal\Core\Entity\EntityInterface $entity |
||
156 | * The entity to be rendered. |
||
157 | * @param array $context |
||
158 | * (optional) Array of context values, corresponding to the attributes on |
||
159 | * the embed HTML tag. |
||
160 | * |
||
161 | * @return string |
||
162 | * The HTML of the entity rendered with the Entity Embed Display plugin. |
||
163 | */ |
||
164 | protected function renderEntityEmbed(EntityInterface $entity, array $context = array()) { |
||
165 | // Support the deprecated view-mode data attribute. |
||
166 | if (isset($context['data-view-mode']) && !isset($context['data-entity-embed-display']) && !isset($context['data-entity-embed-settings'])) { |
||
167 | $context['data-entity-embed-display'] = 'entity_reference:entity_reference_entity_view'; |
||
168 | $context['data-entity-embed-settings'] = ['view_mode' => &$context['data-view-mode']]; |
||
169 | } |
||
170 | |||
171 | // Merge in default attributes. |
||
172 | $context += array( |
||
173 | 'data-entity-id' => $entity->id(), |
||
174 | 'data-entity-type' => $entity->getEntityTypeId(), |
||
175 | 'data-entity-embed-display' => 'entity_reference:entity_reference_entity_view', |
||
176 | 'data-entity-embed-settings' => array(), |
||
177 | ); |
||
178 | |||
179 | // The default Entity Embed Display plugin has been deprecated by the |
||
180 | // rendered entity field formatter. |
||
181 | if ($context['data-entity-embed-display'] === 'default') { |
||
182 | $context['data-entity-embed-display'] = 'entity_reference:entity_reference_entity_view'; |
||
183 | } |
||
184 | |||
185 | // Allow modules to alter the entity prior to embed rendering. |
||
186 | $this->moduleHandler()->alter(array("{$context['data-entity-type']}_embed_context", 'entity_embed_context'), $context, $entity); |
||
187 | |||
188 | // Build and render the Entity Embed Display plugin, allowing modules to |
||
189 | // alter the result before rendering. |
||
190 | $build = $this->renderEntityEmbedDisplayPlugin( |
||
191 | $entity, |
||
192 | $context['data-entity-embed-display'], |
||
193 | $context['data-entity-embed-settings'], |
||
194 | $context |
||
195 | ); |
||
196 | |||
197 | // Maintain data-align if it is there |
||
198 | if (isset($context['data-align'])) { |
||
199 | $build['#attributes']['data-align'] = $context['data-align']; |
||
200 | } |
||
201 | elseif ((isset($context['class']))) { |
||
202 | $build['#attributes']['class'][] = $context['class']; |
||
203 | } |
||
204 | |||
205 | // @todo Should this hook get invoked if $build is an empty array? |
||
206 | $this->moduleHandler()->alter(array("{$context['data-entity-type']}_embed", 'entity_embed'), $build, $entity, $context); |
||
207 | $entity_output = $this->renderer()->render($build); |
||
208 | |||
209 | return $entity_output; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Renders an entity using an Entity Embed Display plugin. |
||
214 | * |
||
215 | * @param \Drupal\Core\Entity\EntityInterface $entity |
||
216 | * The entity to be rendered. |
||
217 | * @param string $plugin_id |
||
218 | * The Entity Embed Display plugin ID. |
||
219 | * @param array $plugin_configuration |
||
220 | * (optional) Array of plugin configuration values. |
||
221 | * @param array $context |
||
222 | * (optional) Array of additional context values, usually the embed HTML |
||
223 | * tag's attributes. |
||
224 | * |
||
225 | * @return array |
||
226 | * A render array for the Entity Embed Display plugin. |
||
227 | */ |
||
228 | protected function renderEntityEmbedDisplayPlugin(EntityInterface $entity, $plugin_id, array $plugin_configuration = array(), array $context = array()) { |
||
243 | |||
244 | /** |
||
245 | * Returns the entity manager. |
||
246 | * |
||
247 | * @return \Drupal\Core\Entity\EntityManagerInterface |
||
248 | * The entity manager. |
||
249 | */ |
||
250 | protected function entityManager() { |
||
256 | |||
257 | /** |
||
258 | * Sets the entity manager service. |
||
259 | * |
||
260 | * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager |
||
261 | * The entity manager service. |
||
262 | * |
||
263 | * @return self |
||
264 | */ |
||
265 | public function setEntityManager(EntityManagerInterface $entity_manager) { |
||
269 | |||
270 | /** |
||
271 | * Returns the module handler. |
||
272 | * |
||
273 | * @return \Drupal\Core\Extension\ModuleHandlerInterface |
||
274 | * The module handler. |
||
275 | */ |
||
276 | protected function moduleHandler() { |
||
282 | |||
283 | /** |
||
284 | * Sets the module handler service. |
||
285 | * |
||
286 | * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler |
||
287 | * The module handler service. |
||
288 | * |
||
289 | * @return self |
||
290 | */ |
||
291 | public function setModuleHandler(ModuleHandlerInterface $module_handler) { |
||
295 | |||
296 | /** |
||
297 | * Returns the Entity Embed Display plugin manager. |
||
298 | * |
||
299 | * @return \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager |
||
300 | * The Entity Embed Display plugin manager. |
||
301 | */ |
||
302 | protected function displayPluginManager() { |
||
308 | |||
309 | /** |
||
310 | * Sets the Entity Embed Display plugin manager service. |
||
311 | * |
||
312 | * @param \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager $display_plugin_manager |
||
313 | * The Entity Embed Display plugin manager service. |
||
314 | * |
||
315 | * @return self |
||
316 | */ |
||
317 | public function setDisplayPluginManager(EntityEmbedDisplayManager $display_plugin_manager) { |
||
321 | |||
322 | /** |
||
323 | * Returns the renderer. |
||
324 | * |
||
325 | * @return \Drupal\Core\Render\RendererInterface |
||
326 | * The renderer. |
||
327 | */ |
||
328 | protected function renderer() { |
||
334 | |||
335 | /** |
||
336 | * Sets the renderer. |
||
337 | * |
||
338 | * @param \Drupal\Core\Render\RendererInterface $renderer |
||
339 | * The renderer. |
||
340 | * |
||
341 | * @return self |
||
342 | */ |
||
343 | public function setRenderer(RendererInterface $renderer) { |
||
347 | } |
||
348 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..