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