Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Entity API for handling entities like nodes or users.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@0
|
8 use Drupal\Core\Entity\EntityInterface;
|
Chris@0
|
9 use Drupal\Core\Entity\Entity\EntityFormDisplay;
|
Chris@0
|
10 use Drupal\Core\Entity\Entity\EntityViewDisplay;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Clears the entity render cache for all entity types.
|
Chris@0
|
14 */
|
Chris@0
|
15 function entity_render_cache_clear() {
|
Chris@0
|
16 $entity_manager = Drupal::entityManager();
|
Chris@0
|
17 foreach ($entity_manager->getDefinitions() as $entity_type => $info) {
|
Chris@0
|
18 if ($entity_manager->hasHandler($entity_type, 'view_builder')) {
|
Chris@0
|
19 $entity_manager->getViewBuilder($entity_type)->resetCache();
|
Chris@0
|
20 }
|
Chris@0
|
21 }
|
Chris@0
|
22 }
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * Returns the entity bundle info.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @param string|null $entity_type
|
Chris@0
|
28 * The entity type whose bundle info should be returned, or NULL for all
|
Chris@0
|
29 * bundles info. Defaults to NULL.
|
Chris@0
|
30 *
|
Chris@0
|
31 * @return array
|
Chris@0
|
32 * The bundle info for a specific entity type, or all entity types.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @deprecated in Drupal 8.x-dev and will be removed before Drupal 9.0.0. Use
|
Chris@0
|
35 * \Drupal\Core\Entity\EntityTypeBundleInfoInterface::getBundleInfo() for a
|
Chris@0
|
36 * single bundle, or
|
Chris@0
|
37 * \Drupal\Core\Entity\EntityTypeBundleInfoInterface::getAllBundleInfo() for
|
Chris@0
|
38 * all bundles.
|
Chris@0
|
39 *
|
Chris@0
|
40 * @see \Drupal\Core\Entity\EntityTypeBundleInfoInterface::getBundleInfo()
|
Chris@0
|
41 * @see \Drupal\Core\Entity\EntityTypeBundleInfoInterface::getAllBundleInfo()
|
Chris@0
|
42 */
|
Chris@0
|
43 function entity_get_bundles($entity_type = NULL) {
|
Chris@0
|
44 if (isset($entity_type)) {
|
Chris@0
|
45 return \Drupal::entityManager()->getBundleInfo($entity_type);
|
Chris@0
|
46 }
|
Chris@0
|
47 else {
|
Chris@0
|
48 return \Drupal::entityManager()->getAllBundleInfo();
|
Chris@0
|
49 }
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * Loads an entity from the database.
|
Chris@0
|
54 *
|
Chris@0
|
55 * @param string $entity_type
|
Chris@0
|
56 * The entity type to load, e.g. node or user.
|
Chris@0
|
57 * @param mixed $id
|
Chris@0
|
58 * The id of the entity to load.
|
Chris@0
|
59 * @param bool $reset
|
Chris@0
|
60 * Whether to reset the internal cache for the requested entity type.
|
Chris@0
|
61 *
|
Chris@0
|
62 * @return \Drupal\Core\Entity\EntityInterface|null
|
Chris@0
|
63 * The entity object, or NULL if there is no entity with the given ID.
|
Chris@0
|
64 *
|
Chris@0
|
65 * @deprecated in Drupal 8.0.x, will be removed before Drupal 9.0.0. Use
|
Chris@0
|
66 * The method overriding Entity::load() for the entity type, e.g.
|
Chris@0
|
67 * \Drupal\node\Entity\Node::load() if the entity type is known. If the
|
Chris@0
|
68 * entity type is variable, use the entity manager service to load the entity
|
Chris@0
|
69 * from the entity storage:
|
Chris@0
|
70 * @code
|
Chris@0
|
71 * \Drupal::entityTypeManager()->getStorage($entity_type)->load($id);
|
Chris@0
|
72 * @endcode
|
Chris@0
|
73 *
|
Chris@0
|
74 * @see \Drupal\Core\Entity\EntityInterface::load()
|
Chris@0
|
75 * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getStorage()
|
Chris@0
|
76 * @see \Drupal\Core\Entity\EntityStorageInterface::load()
|
Chris@0
|
77 * @see \Drupal\Core\Entity\Sql\SqlContentEntityStorage
|
Chris@0
|
78 * @see \Drupal\Core\Entity\Query\QueryInterface
|
Chris@0
|
79 */
|
Chris@0
|
80 function entity_load($entity_type, $id, $reset = FALSE) {
|
Chris@0
|
81 $controller = \Drupal::entityManager()->getStorage($entity_type);
|
Chris@0
|
82 if ($reset) {
|
Chris@0
|
83 $controller->resetCache([$id]);
|
Chris@0
|
84 }
|
Chris@0
|
85 return $controller->load($id);
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 /**
|
Chris@0
|
89 * Loads an entity from the database.
|
Chris@0
|
90 *
|
Chris@0
|
91 * @param string $entity_type
|
Chris@0
|
92 * The entity type to load, e.g. node or user.
|
Chris@0
|
93 * @param int $revision_id
|
Chris@0
|
94 * The id of the entity to load.
|
Chris@0
|
95 *
|
Chris@0
|
96 * @return \Drupal\Core\Entity\EntityInterface|null
|
Chris@0
|
97 * The entity object, or NULL if there is no entity with the given revision
|
Chris@0
|
98 * id.
|
Chris@0
|
99 *
|
Chris@0
|
100 * @deprecated as of Drupal 8.0.x, will be removed before Drupal 9.0.0. Use
|
Chris@0
|
101 * the entity storage's loadRevision() method to load a specific entity
|
Chris@0
|
102 * revision:
|
Chris@0
|
103 * @code
|
Chris@0
|
104 * \Drupal::entityTypeManager()
|
Chris@0
|
105 * ->getStorage($entity_type)
|
Chris@0
|
106 * ->loadRevision($revision_id);
|
Chris@0
|
107 * @endcode
|
Chris@0
|
108 *
|
Chris@0
|
109 * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getStorage()
|
Chris@0
|
110 * @see \Drupal\Core\Entity\EntityStorageInterface::loadRevision()
|
Chris@0
|
111 * @see \Drupal\Core\Entity\Sql\SqlContentEntityStorage
|
Chris@0
|
112 */
|
Chris@0
|
113 function entity_revision_load($entity_type, $revision_id) {
|
Chris@0
|
114 return \Drupal::entityManager()
|
Chris@0
|
115 ->getStorage($entity_type)
|
Chris@0
|
116 ->loadRevision($revision_id);
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 /**
|
Chris@0
|
120 * Deletes an entity revision.
|
Chris@0
|
121 *
|
Chris@0
|
122 * @param string $entity_type
|
Chris@0
|
123 * The entity type to load, e.g. node or user.
|
Chris@0
|
124 * @param $revision_id
|
Chris@0
|
125 * The revision ID to delete.
|
Chris@0
|
126 *
|
Chris@0
|
127 * @deprecated as of Drupal 8.0.x, will be removed before Drupal 9.0.0. Use
|
Chris@0
|
128 * the entity storage's deleteRevision() method to delete a specific entity
|
Chris@0
|
129 * revision:
|
Chris@0
|
130 * @code
|
Chris@0
|
131 * \Drupal::entityTypeManager()
|
Chris@0
|
132 * ->getStorage($entity_type)
|
Chris@0
|
133 * ->deleteRevision($revision_id);
|
Chris@0
|
134 * @endcode
|
Chris@0
|
135 *
|
Chris@0
|
136 * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getStorage()
|
Chris@0
|
137 * @see \Drupal\Core\Entity\EntityStorageInterface::deleteRevision()
|
Chris@0
|
138 */
|
Chris@0
|
139 function entity_revision_delete($entity_type, $revision_id) {
|
Chris@0
|
140 \Drupal::entityManager()
|
Chris@0
|
141 ->getStorage($entity_type)
|
Chris@0
|
142 ->deleteRevision($revision_id);
|
Chris@0
|
143 }
|
Chris@0
|
144
|
Chris@0
|
145 /**
|
Chris@0
|
146 * Loads multiple entities from the database.
|
Chris@0
|
147 *
|
Chris@0
|
148 * This function should be used whenever you need to load more than one entity
|
Chris@0
|
149 * from the database. The entities are loaded into memory and will not require
|
Chris@0
|
150 * database access if loaded again during the same page request.
|
Chris@0
|
151 *
|
Chris@0
|
152 * The actual loading is done through a class that has to implement the
|
Chris@0
|
153 * \Drupal\Core\Entity\EntityStorageInterface interface. By default,
|
Chris@0
|
154 * \Drupal\Core\Entity\Sql\SqlContentEntityStorage is used for content entities
|
Chris@0
|
155 * and Drupal\Core\Config\Entity\ConfigEntityStorage for config entities. Entity
|
Chris@0
|
156 * types can specify that a different class should be used by setting the
|
Chris@0
|
157 * "handlers['storage']" key in the entity plugin annotation. These classes
|
Chris@0
|
158 * can either implement the \Drupal\Core\Entity\EntityStorageInterface
|
Chris@0
|
159 * interface, or, most commonly, extend the
|
Chris@0
|
160 * \Drupal\Core\Entity\Sql\SqlContentEntityStorage class. See
|
Chris@0
|
161 * \Drupal\node\Entity\Node and \Drupal\node\NodeStorage for an example.
|
Chris@0
|
162 *
|
Chris@0
|
163 * @param string $entity_type
|
Chris@0
|
164 * The entity type to load, e.g. node or user.
|
Chris@0
|
165 * @param array $ids
|
Chris@0
|
166 * (optional) An array of entity IDs. If omitted, all entities are loaded.
|
Chris@0
|
167 * @param bool $reset
|
Chris@0
|
168 * Whether to reset the internal cache for the requested entity type.
|
Chris@0
|
169 *
|
Chris@0
|
170 * @return array
|
Chris@0
|
171 * An array of entity objects indexed by their IDs.
|
Chris@0
|
172 *
|
Chris@0
|
173 * @deprecated in Drupal 8.0.x, will be removed before Drupal 9.0.0. Use
|
Chris@0
|
174 * The method overriding Entity::loadMultiple() for the entity type, e.g.
|
Chris@0
|
175 * \Drupal\node\Entity\Node::loadMultiple() if the entity type is known. If
|
Chris@0
|
176 * the entity type is variable, use the entity manager service to load the
|
Chris@0
|
177 * entity from the entity storage:
|
Chris@0
|
178 * @code
|
Chris@0
|
179 * \Drupal::entityTypeManager()->getStorage($entity_type)->loadMultiple($id);
|
Chris@0
|
180 * @endcode
|
Chris@0
|
181 *
|
Chris@0
|
182 * @see \Drupal\Core\Entity\EntityInterface::loadMultiple()
|
Chris@0
|
183 * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getStorage()
|
Chris@0
|
184 * @see \Drupal\Core\Entity\EntityStorageInterface::loadMultiple()
|
Chris@0
|
185 * @see \Drupal\Core\Entity\Sql\SqlContentEntityStorage
|
Chris@0
|
186 * @see \Drupal\Core\Entity\Query\QueryInterface
|
Chris@0
|
187 */
|
Chris@0
|
188 function entity_load_multiple($entity_type, array $ids = NULL, $reset = FALSE) {
|
Chris@0
|
189 $controller = \Drupal::entityManager()->getStorage($entity_type);
|
Chris@0
|
190 if ($reset) {
|
Chris@0
|
191 $controller->resetCache($ids);
|
Chris@0
|
192 }
|
Chris@0
|
193 return $controller->loadMultiple($ids);
|
Chris@0
|
194 }
|
Chris@0
|
195
|
Chris@0
|
196 /**
|
Chris@0
|
197 * Load entities by their property values.
|
Chris@0
|
198 *
|
Chris@0
|
199 * @param string $entity_type
|
Chris@0
|
200 * The entity type to load, e.g. node or user.
|
Chris@0
|
201 * @param array $values
|
Chris@0
|
202 * An associative array where the keys are the property names and the
|
Chris@0
|
203 * values are the values those properties must have.
|
Chris@0
|
204 *
|
Chris@0
|
205 * @return array
|
Chris@0
|
206 * An array of entity objects indexed by their IDs. Returns an empty array if
|
Chris@0
|
207 * no matching entities are found.
|
Chris@0
|
208 *
|
Chris@0
|
209 * @deprecated as of Drupal 8.0.x, will be removed before Drupal 9.0.0. Use
|
Chris@0
|
210 * the entity storage's loadByProperties() method to load an entity by their
|
Chris@0
|
211 * property values:
|
Chris@0
|
212 * @code
|
Chris@0
|
213 * \Drupal::entityTypeManager()
|
Chris@0
|
214 * ->getStorage($entity_type)
|
Chris@0
|
215 * ->loadByProperties($values);
|
Chris@0
|
216 * @endcode
|
Chris@0
|
217 *
|
Chris@0
|
218 * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getStorage()
|
Chris@0
|
219 * @see \Drupal\Core\Entity\EntityStorageInterface::loadByProperties()
|
Chris@0
|
220 */
|
Chris@0
|
221 function entity_load_multiple_by_properties($entity_type, array $values) {
|
Chris@0
|
222 return \Drupal::entityManager()
|
Chris@0
|
223 ->getStorage($entity_type)
|
Chris@0
|
224 ->loadByProperties($values);
|
Chris@0
|
225 }
|
Chris@0
|
226
|
Chris@0
|
227 /**
|
Chris@0
|
228 * Loads the unchanged, i.e. not modified, entity from the database.
|
Chris@0
|
229 *
|
Chris@0
|
230 * Unlike entity_load() this function ensures the entity is directly loaded from
|
Chris@0
|
231 * the database, thus bypassing any static cache. In particular, this function
|
Chris@0
|
232 * is useful to determine changes by comparing the entity being saved to the
|
Chris@0
|
233 * stored entity.
|
Chris@0
|
234 *
|
Chris@0
|
235 * @param $entity_type
|
Chris@0
|
236 * The entity type to load, e.g. node or user.
|
Chris@0
|
237 * @param $id
|
Chris@0
|
238 * The ID of the entity to load.
|
Chris@0
|
239 *
|
Chris@0
|
240 * @return \Drupal\Core\Entity\EntityInterface|null
|
Chris@0
|
241 * The unchanged entity, or FALSE if the entity cannot be loaded.
|
Chris@0
|
242 *
|
Chris@0
|
243 * @deprecated as of Drupal 8.0.x, will be removed before Drupal 9.0.0. Use
|
Chris@0
|
244 * the entity storage's loadUnchanged() method to load an unchanged entity:
|
Chris@0
|
245 * @code
|
Chris@0
|
246 * \Drupal::entityTypeManager()->getStorage($entity_type)->loadUnchanged($id);
|
Chris@0
|
247 * @endcode
|
Chris@0
|
248 *
|
Chris@0
|
249 * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getStorage()
|
Chris@0
|
250 * @see \Drupal\Core\Entity\EntityStorageInterface::loadUnchanged()
|
Chris@0
|
251 */
|
Chris@0
|
252 function entity_load_unchanged($entity_type, $id) {
|
Chris@0
|
253 return \Drupal::entityManager()
|
Chris@0
|
254 ->getStorage($entity_type)
|
Chris@0
|
255 ->loadUnchanged($id);
|
Chris@0
|
256 }
|
Chris@0
|
257
|
Chris@0
|
258 /**
|
Chris@0
|
259 * Deletes multiple entities permanently.
|
Chris@0
|
260 *
|
Chris@0
|
261 * @param string $entity_type
|
Chris@0
|
262 * The type of the entity.
|
Chris@0
|
263 * @param array $ids
|
Chris@0
|
264 * An array of entity IDs of the entities to delete.
|
Chris@0
|
265 *
|
Chris@0
|
266 * @deprecated as of Drupal 8.0.x, will be removed before Drupal 9.0.0. Use
|
Chris@0
|
267 * the entity storage's delete() method to delete multiple entities:
|
Chris@0
|
268 * @code
|
Chris@0
|
269 * $storage_handler = \Drupal::entityTypeManager()->getStorage($entity_type);
|
Chris@0
|
270 * $entities = $storage_handler->loadMultiple($ids);
|
Chris@0
|
271 * $storage_handler->delete($entities);
|
Chris@0
|
272 * @endcode
|
Chris@0
|
273 *
|
Chris@0
|
274 * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getStorage()
|
Chris@0
|
275 * @see \Drupal\Core\Entity\EntityStorageInterface::loadMultiple()
|
Chris@0
|
276 * @see \Drupal\Core\Entity\EntityStorageInterface::delete()
|
Chris@0
|
277 */
|
Chris@0
|
278 function entity_delete_multiple($entity_type, array $ids) {
|
Chris@0
|
279 $controller = \Drupal::entityManager()->getStorage($entity_type);
|
Chris@0
|
280 $entities = $controller->loadMultiple($ids);
|
Chris@0
|
281 $controller->delete($entities);
|
Chris@0
|
282 }
|
Chris@0
|
283
|
Chris@0
|
284 /**
|
Chris@0
|
285 * Constructs a new entity object, without permanently saving it.
|
Chris@0
|
286 *
|
Chris@0
|
287 * @param string $entity_type
|
Chris@0
|
288 * The type of the entity.
|
Chris@0
|
289 * @param array $values
|
Chris@0
|
290 * (optional) An array of values to set, keyed by property name. If the
|
Chris@0
|
291 * entity type has bundles, the bundle key has to be specified.
|
Chris@0
|
292 *
|
Chris@0
|
293 * @return \Drupal\Core\Entity\EntityInterface
|
Chris@0
|
294 * A new entity object.
|
Chris@0
|
295 *
|
Chris@0
|
296 * @deprecated in Drupal 8.0.x, will be removed before Drupal 9.0.0. Use
|
Chris@0
|
297 * The method overriding Entity::create() for the entity type, e.g.
|
Chris@0
|
298 * \Drupal\node\Entity\Node::create() if the entity type is known. If the
|
Chris@0
|
299 * entity type is variable, use the entity storage's create() method to
|
Chris@0
|
300 * construct a new entity:
|
Chris@0
|
301 * @code
|
Chris@0
|
302 * \Drupal::entityTypeManager()->getStorage($entity_type)->create($values);
|
Chris@0
|
303 * @endcode
|
Chris@0
|
304 *
|
Chris@0
|
305 * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getStorage()
|
Chris@0
|
306 * @see \Drupal\Core\Entity\EntityStorageInterface::create()
|
Chris@0
|
307 */
|
Chris@0
|
308 function entity_create($entity_type, array $values = []) {
|
Chris@0
|
309 return \Drupal::entityManager()
|
Chris@0
|
310 ->getStorage($entity_type)
|
Chris@0
|
311 ->create($values);
|
Chris@0
|
312 }
|
Chris@0
|
313
|
Chris@0
|
314 /**
|
Chris@0
|
315 * Returns the label of an entity.
|
Chris@0
|
316 *
|
Chris@0
|
317 * @param \Drupal\Core\Entity\EntityInterface $entity
|
Chris@0
|
318 * The entity for which to generate the label.
|
Chris@0
|
319 * @param $langcode
|
Chris@0
|
320 * (optional) The language code of the language that should be used for
|
Chris@0
|
321 * getting the label. If set to NULL, the entity's default language is
|
Chris@0
|
322 * used.
|
Chris@0
|
323 *
|
Chris@0
|
324 * @return string|null
|
Chris@0
|
325 * The label of the entity, or NULL if there is no label defined.
|
Chris@0
|
326 *
|
Chris@0
|
327 * @deprecated as of Drupal 8.0.x, will be removed before Drupal 9.0.0. Use
|
Chris@0
|
328 * the entity's label() method to get the label of the entity:
|
Chris@0
|
329 * @code
|
Chris@0
|
330 * $entity->label($langcode);
|
Chris@0
|
331 * @endcode
|
Chris@0
|
332 *
|
Chris@0
|
333 * @see \Drupal\Core\Entity\EntityInterface::label()
|
Chris@0
|
334 */
|
Chris@0
|
335 function entity_page_label(EntityInterface $entity, $langcode = NULL) {
|
Chris@0
|
336 return $entity->label($langcode);
|
Chris@0
|
337 }
|
Chris@0
|
338
|
Chris@0
|
339 /**
|
Chris@0
|
340 * Returns the render array for an entity.
|
Chris@0
|
341 *
|
Chris@0
|
342 * @param \Drupal\Core\Entity\EntityInterface $entity
|
Chris@0
|
343 * The entity to be rendered.
|
Chris@0
|
344 * @param string $view_mode
|
Chris@0
|
345 * The view mode that should be used to display the entity.
|
Chris@0
|
346 * @param string $langcode
|
Chris@0
|
347 * (optional) For which language the entity should be rendered, defaults to
|
Chris@0
|
348 * the current content language.
|
Chris@0
|
349 * @param bool $reset
|
Chris@0
|
350 * (optional) Whether to reset the render cache for the requested entity.
|
Chris@0
|
351 * Defaults to FALSE.
|
Chris@0
|
352 *
|
Chris@0
|
353 * @return array
|
Chris@0
|
354 * A render array for the entity.
|
Chris@0
|
355 *
|
Chris@0
|
356 * @deprecated as of Drupal 8.0.x, will be removed before Drupal 9.0.0.
|
Chris@0
|
357 * Use the entity view builder's view() method for creating a render array:
|
Chris@0
|
358 * @code
|
Chris@0
|
359 * $view_builder = \Drupal::entityTypeManager()
|
Chris@0
|
360 * ->getViewBuilder($entity->getEntityTypeId());
|
Chris@0
|
361 * return $view_builder->view($entity, $view_mode, $langcode);
|
Chris@0
|
362 * @endcode
|
Chris@0
|
363 *
|
Chris@0
|
364 * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getViewBuilder()
|
Chris@0
|
365 * @see \Drupal\Core\Entity\EntityViewBuilderInterface::view()
|
Chris@0
|
366 */
|
Chris@0
|
367 function entity_view(EntityInterface $entity, $view_mode, $langcode = NULL, $reset = FALSE) {
|
Chris@0
|
368 $render_controller = \Drupal::entityManager()->getViewBuilder($entity->getEntityTypeId());
|
Chris@0
|
369 if ($reset) {
|
Chris@0
|
370 $render_controller->resetCache([$entity]);
|
Chris@0
|
371 }
|
Chris@0
|
372 return $render_controller->view($entity, $view_mode, $langcode);
|
Chris@0
|
373 }
|
Chris@0
|
374
|
Chris@0
|
375 /**
|
Chris@0
|
376 * Returns the render array for the provided entities.
|
Chris@0
|
377 *
|
Chris@0
|
378 * @param \Drupal\Core\Entity\EntityInterface[] $entities
|
Chris@0
|
379 * The entities to be rendered, must be of the same type.
|
Chris@0
|
380 * @param string $view_mode
|
Chris@0
|
381 * The view mode that should be used to display the entity.
|
Chris@0
|
382 * @param string $langcode
|
Chris@0
|
383 * (optional) For which language the entity should be rendered, defaults to
|
Chris@0
|
384 * the current content language.
|
Chris@0
|
385 * @param bool $reset
|
Chris@0
|
386 * (optional) Whether to reset the render cache for the requested entities.
|
Chris@0
|
387 * Defaults to FALSE.
|
Chris@0
|
388 *
|
Chris@0
|
389 * @return array
|
Chris@0
|
390 * A render array for the entities, indexed by the same keys as the
|
Chris@0
|
391 * entities array passed in $entities.
|
Chris@0
|
392 *
|
Chris@0
|
393 * @deprecated as of Drupal 8.0.x, will be removed before Drupal 9.0.0.
|
Chris@0
|
394 * Use the entity view builder's viewMultiple() method for creating a render
|
Chris@0
|
395 * array for the provided entities:
|
Chris@0
|
396 * @code
|
Chris@0
|
397 * $view_builder = \Drupal::entityTypeManager()
|
Chris@0
|
398 * ->getViewBuilder($entity->getEntityTypeId());
|
Chris@0
|
399 * return $view_builder->viewMultiple($entities, $view_mode, $langcode);
|
Chris@0
|
400 * @endcode
|
Chris@0
|
401 *
|
Chris@0
|
402 * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getViewBuilder()
|
Chris@0
|
403 * @see \Drupal\Core\Entity\EntityViewBuilderInterface::viewMultiple()
|
Chris@0
|
404 */
|
Chris@0
|
405 function entity_view_multiple(array $entities, $view_mode, $langcode = NULL, $reset = FALSE) {
|
Chris@0
|
406 $render_controller = \Drupal::entityManager()->getViewBuilder(reset($entities)->getEntityTypeId());
|
Chris@0
|
407 if ($reset) {
|
Chris@0
|
408 $render_controller->resetCache($entities);
|
Chris@0
|
409 }
|
Chris@0
|
410 return $render_controller->viewMultiple($entities, $view_mode, $langcode);
|
Chris@0
|
411 }
|
Chris@0
|
412
|
Chris@0
|
413 /**
|
Chris@0
|
414 * Returns the entity view display associated with a bundle and view mode.
|
Chris@0
|
415 *
|
Chris@0
|
416 * Use this function when assigning suggested display options for a component
|
Chris@0
|
417 * in a given view mode. Note that they will only be actually used at render
|
Chris@0
|
418 * time if the view mode itself is configured to use dedicated display settings
|
Chris@0
|
419 * for the bundle; if not, the 'default' display is used instead.
|
Chris@0
|
420 *
|
Chris@0
|
421 * The function reads the entity view display from the current configuration, or
|
Chris@0
|
422 * returns a ready-to-use empty one if configuration entry exists yet for this
|
Chris@0
|
423 * bundle and view mode. This streamlines manipulation of display objects by
|
Chris@0
|
424 * always returning a consistent object that reflects the current state of the
|
Chris@0
|
425 * configuration.
|
Chris@0
|
426 *
|
Chris@0
|
427 * Example usage:
|
Chris@0
|
428 * - Set the 'body' field to be displayed and the 'field_image' field to be
|
Chris@0
|
429 * hidden on article nodes in the 'default' display.
|
Chris@0
|
430 * @code
|
Chris@0
|
431 * entity_get_display('node', 'article', 'default')
|
Chris@0
|
432 * ->setComponent('body', array(
|
Chris@0
|
433 * 'type' => 'text_summary_or_trimmed',
|
Chris@0
|
434 * 'settings' => array('trim_length' => '200')
|
Chris@0
|
435 * 'weight' => 1,
|
Chris@0
|
436 * ))
|
Chris@0
|
437 * ->removeComponent('field_image')
|
Chris@0
|
438 * ->save();
|
Chris@0
|
439 * @endcode
|
Chris@0
|
440 *
|
Chris@0
|
441 * @param string $entity_type
|
Chris@0
|
442 * The entity type.
|
Chris@0
|
443 * @param string $bundle
|
Chris@0
|
444 * The bundle.
|
Chris@0
|
445 * @param string $view_mode
|
Chris@0
|
446 * The view mode, or 'default' to retrieve the 'default' display object for
|
Chris@0
|
447 * this bundle.
|
Chris@0
|
448 *
|
Chris@0
|
449 * @return \Drupal\Core\Entity\Display\EntityViewDisplayInterface
|
Chris@0
|
450 * The entity view display associated with the view mode.
|
Chris@0
|
451 *
|
Chris@0
|
452 * @deprecated as of Drupal 8.0.x, will be removed before Drupal 9.0.0.
|
Chris@0
|
453 * If the display is available in configuration use:
|
Chris@0
|
454 * @code
|
Chris@0
|
455 * \Drupal::entityTypeManager()
|
Chris@0
|
456 * ->getStorage('entity_view_display')
|
Chris@0
|
457 * ->load($entity_type . '.' . $bundle . '.' . $view_mode);
|
Chris@0
|
458 * @endcode
|
Chris@0
|
459 * When the display is not available in configuration, you can create a new
|
Chris@0
|
460 * EntityViewDisplay object using:
|
Chris@0
|
461 * @code
|
Chris@0
|
462 * $values = array(
|
Chris@0
|
463 * 'targetEntityType' => $entity_type,
|
Chris@0
|
464 * 'bundle' => $bundle,
|
Chris@0
|
465 * 'mode' => $view_mode,
|
Chris@0
|
466 * 'status' => TRUE,
|
Chris@0
|
467 * );
|
Chris@0
|
468 * \Drupal::entityTypeManager()
|
Chris@0
|
469 * ->getStorage('entity_view_display')
|
Chris@0
|
470 * ->create($values);
|
Chris@0
|
471 * @endcode
|
Chris@0
|
472 *
|
Chris@0
|
473 * @see \Drupal\Core\Entity\EntityStorageInterface::create()
|
Chris@0
|
474 * @see \Drupal\Core\Entity\EntityStorageInterface::load()
|
Chris@0
|
475 */
|
Chris@0
|
476 function entity_get_display($entity_type, $bundle, $view_mode) {
|
Chris@0
|
477 // Try loading the display from configuration.
|
Chris@0
|
478 $display = EntityViewDisplay::load($entity_type . '.' . $bundle . '.' . $view_mode);
|
Chris@0
|
479
|
Chris@0
|
480 // If not found, create a fresh display object. We do not preemptively create
|
Chris@0
|
481 // new entity_view_display configuration entries for each existing entity type
|
Chris@0
|
482 // and bundle whenever a new view mode becomes available. Instead,
|
Chris@0
|
483 // configuration entries are only created when a display object is explicitly
|
Chris@0
|
484 // configured and saved.
|
Chris@0
|
485 if (!$display) {
|
Chris@0
|
486 $display = EntityViewDisplay::create([
|
Chris@0
|
487 'targetEntityType' => $entity_type,
|
Chris@0
|
488 'bundle' => $bundle,
|
Chris@0
|
489 'mode' => $view_mode,
|
Chris@0
|
490 'status' => TRUE,
|
Chris@0
|
491 ]);
|
Chris@0
|
492 }
|
Chris@0
|
493
|
Chris@0
|
494 return $display;
|
Chris@0
|
495 }
|
Chris@0
|
496
|
Chris@0
|
497 /**
|
Chris@0
|
498 * Returns the entity form display associated with a bundle and form mode.
|
Chris@0
|
499 *
|
Chris@0
|
500 * The function reads the entity form display object from the current
|
Chris@0
|
501 * configuration, or returns a ready-to-use empty one if no configuration entry
|
Chris@0
|
502 * exists yet for this bundle and form mode. This streamlines manipulation of
|
Chris@0
|
503 * entity form displays by always returning a consistent object that reflects
|
Chris@0
|
504 * the current state of the configuration.
|
Chris@0
|
505 *
|
Chris@0
|
506 * Example usage:
|
Chris@0
|
507 * - Set the 'body' field to be displayed with the 'text_textarea_with_summary'
|
Chris@0
|
508 * widget and the 'field_image' field to be hidden on article nodes in the
|
Chris@0
|
509 * 'default' form mode.
|
Chris@0
|
510 * @code
|
Chris@0
|
511 * entity_get_form_display('node', 'article', 'default')
|
Chris@0
|
512 * ->setComponent('body', array(
|
Chris@0
|
513 * 'type' => 'text_textarea_with_summary',
|
Chris@0
|
514 * 'weight' => 1,
|
Chris@0
|
515 * ))
|
Chris@0
|
516 * ->setComponent('field_image', array(
|
Chris@0
|
517 * 'region' => 'hidden',
|
Chris@0
|
518 * ))
|
Chris@0
|
519 * ->save();
|
Chris@0
|
520 * @endcode
|
Chris@0
|
521 *
|
Chris@0
|
522 * @param string $entity_type
|
Chris@0
|
523 * The entity type.
|
Chris@0
|
524 * @param string $bundle
|
Chris@0
|
525 * The bundle.
|
Chris@0
|
526 * @param string $form_mode
|
Chris@0
|
527 * The form mode.
|
Chris@0
|
528 *
|
Chris@0
|
529 * @return \Drupal\Core\Entity\Display\EntityFormDisplayInterface
|
Chris@0
|
530 * The entity form display associated with the given form mode.
|
Chris@0
|
531 *
|
Chris@0
|
532 * @deprecated as of Drupal 8.0.x, will be removed before Drupal 9.0.0.
|
Chris@0
|
533 * If the entity form display is available in configuration use:
|
Chris@0
|
534 * @code
|
Chris@0
|
535 * \Drupal::entityTypeManager()
|
Chris@0
|
536 * ->getStorage('entity_form_display')
|
Chris@0
|
537 * ->load($entity_type . '.' . $bundle . '.' . $form_mode);
|
Chris@0
|
538 * @endcode
|
Chris@0
|
539 * When the entity form display is not available in configuration, you can
|
Chris@0
|
540 * create a new EntityFormDisplay object using:
|
Chris@0
|
541 * @code
|
Chris@0
|
542 * $values = array(
|
Chris@0
|
543 * 'targetEntityType' => $entity_type,
|
Chris@0
|
544 * 'bundle' => $bundle,
|
Chris@0
|
545 * 'mode' => $form_mode,
|
Chris@0
|
546 * 'status' => TRUE,
|
Chris@0
|
547 * );
|
Chris@0
|
548 * \Drupal::entityTypeManager()
|
Chris@0
|
549 * ->getStorage('entity_form_display')
|
Chris@0
|
550 * ->create($values);
|
Chris@0
|
551 * @endcode
|
Chris@0
|
552 *
|
Chris@0
|
553 * @see \Drupal\Core\Entity\EntityStorageInterface::create()
|
Chris@0
|
554 * @see \Drupal\Core\Entity\EntityStorageInterface::load()
|
Chris@0
|
555 */
|
Chris@0
|
556 function entity_get_form_display($entity_type, $bundle, $form_mode) {
|
Chris@0
|
557 // Try loading the entity from configuration.
|
Chris@0
|
558 $entity_form_display = EntityFormDisplay::load($entity_type . '.' . $bundle . '.' . $form_mode);
|
Chris@0
|
559
|
Chris@0
|
560 // If not found, create a fresh entity object. We do not preemptively create
|
Chris@0
|
561 // new entity form display configuration entries for each existing entity type
|
Chris@0
|
562 // and bundle whenever a new form mode becomes available. Instead,
|
Chris@0
|
563 // configuration entries are only created when an entity form display is
|
Chris@0
|
564 // explicitly configured and saved.
|
Chris@0
|
565 if (!$entity_form_display) {
|
Chris@0
|
566 $entity_form_display = EntityFormDisplay::create([
|
Chris@0
|
567 'targetEntityType' => $entity_type,
|
Chris@0
|
568 'bundle' => $bundle,
|
Chris@0
|
569 'mode' => $form_mode,
|
Chris@0
|
570 'status' => TRUE,
|
Chris@0
|
571 ]);
|
Chris@0
|
572 }
|
Chris@0
|
573
|
Chris@0
|
574 return $entity_form_display;
|
Chris@0
|
575 }
|