comparison core/lib/Drupal/Core/Entity/EntityViewBuilder.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Core\Entity;
4
5 use Drupal\Component\Utility\Crypt;
6 use Drupal\Core\Cache\Cache;
7 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
8 use Drupal\Core\Entity\Entity\EntityViewDisplay;
9 use Drupal\Core\Field\FieldItemInterface;
10 use Drupal\Core\Field\FieldItemListInterface;
11 use Drupal\Core\Language\LanguageManagerInterface;
12 use Drupal\Core\Render\Element;
13 use Drupal\Core\Theme\Registry;
14 use Drupal\Core\TypedData\TranslatableInterface;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
16
17 /**
18 * Base class for entity view builders.
19 *
20 * @ingroup entity_api
21 */
22 class EntityViewBuilder extends EntityHandlerBase implements EntityHandlerInterface, EntityViewBuilderInterface {
23
24 /**
25 * The type of entities for which this view builder is instantiated.
26 *
27 * @var string
28 */
29 protected $entityTypeId;
30
31 /**
32 * Information about the entity type.
33 *
34 * @var \Drupal\Core\Entity\EntityTypeInterface
35 */
36 protected $entityType;
37
38 /**
39 * The entity manager service.
40 *
41 * @var \Drupal\Core\Entity\EntityManagerInterface
42 */
43 protected $entityManager;
44
45 /**
46 * The cache bin used to store the render cache.
47 *
48 * @var string
49 */
50 protected $cacheBin = 'render';
51
52 /**
53 * The language manager.
54 *
55 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
56 */
57 protected $languageManager;
58
59 /**
60 * The theme registry.
61 *
62 * @var \Drupal\Core\Theme\Registry
63 */
64 protected $themeRegistry;
65
66 /**
67 * The EntityViewDisplay objects created for individual field rendering.
68 *
69 * @see \Drupal\Core\Entity\EntityViewBuilder::getSingleFieldDisplay()
70 *
71 * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface[]
72 */
73 protected $singleFieldDisplays;
74
75 /**
76 * Constructs a new EntityViewBuilder.
77 *
78 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
79 * The entity type definition.
80 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
81 * The entity manager service.
82 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
83 * The language manager.
84 * @param \Drupal\Core\Theme\Registry $theme_registry
85 * The theme registry.
86 */
87 public function __construct(EntityTypeInterface $entity_type, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager, Registry $theme_registry = NULL) {
88 $this->entityTypeId = $entity_type->id();
89 $this->entityType = $entity_type;
90 $this->entityManager = $entity_manager;
91 $this->languageManager = $language_manager;
92 $this->themeRegistry = $theme_registry ?: \Drupal::service('theme.registry');
93 }
94
95 /**
96 * {@inheritdoc}
97 */
98 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
99 return new static(
100 $entity_type,
101 $container->get('entity.manager'),
102 $container->get('language_manager'),
103 $container->get('theme.registry')
104 );
105 }
106
107 /**
108 * {@inheritdoc}
109 */
110 public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) {
111 $build_list = $this->viewMultiple([$entity], $view_mode, $langcode);
112
113 // The default ::buildMultiple() #pre_render callback won't run, because we
114 // extract a child element of the default renderable array. Thus we must
115 // assign an alternative #pre_render callback that applies the necessary
116 // transformations and then still calls ::buildMultiple().
117 $build = $build_list[0];
118 $build['#pre_render'][] = [$this, 'build'];
119
120 return $build;
121 }
122
123 /**
124 * {@inheritdoc}
125 */
126 public function viewMultiple(array $entities = [], $view_mode = 'full', $langcode = NULL) {
127 $build_list = [
128 '#sorted' => TRUE,
129 '#pre_render' => [[$this, 'buildMultiple']],
130 ];
131 $weight = 0;
132 foreach ($entities as $key => $entity) {
133 // Ensure that from now on we are dealing with the proper translation
134 // object.
135 $entity = $this->entityManager->getTranslationFromContext($entity, $langcode);
136
137 // Set build defaults.
138 $build_list[$key] = $this->getBuildDefaults($entity, $view_mode);
139 $entityType = $this->entityTypeId;
140 $this->moduleHandler()->alter([$entityType . '_build_defaults', 'entity_build_defaults'], $build_list[$key], $entity, $view_mode);
141
142 $build_list[$key]['#weight'] = $weight++;
143 }
144
145 return $build_list;
146 }
147
148 /**
149 * Provides entity-specific defaults to the build process.
150 *
151 * @param \Drupal\Core\Entity\EntityInterface $entity
152 * The entity for which the defaults should be provided.
153 * @param string $view_mode
154 * The view mode that should be used.
155 *
156 * @return array
157 */
158 protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
159 // Allow modules to change the view mode.
160 $context = [];
161 $this->moduleHandler()->alter('entity_view_mode', $view_mode, $entity, $context);
162
163 $build = [
164 "#{$this->entityTypeId}" => $entity,
165 '#view_mode' => $view_mode,
166 // Collect cache defaults for this entity.
167 '#cache' => [
168 'tags' => Cache::mergeTags($this->getCacheTags(), $entity->getCacheTags()),
169 'contexts' => $entity->getCacheContexts(),
170 'max-age' => $entity->getCacheMaxAge(),
171 ],
172 ];
173
174 // Add the default #theme key if a template exists for it.
175 if ($this->themeRegistry->getRuntime()->has($this->entityTypeId)) {
176 $build['#theme'] = $this->entityTypeId;
177 }
178
179 // Cache the rendered output if permitted by the view mode and global entity
180 // type configuration.
181 if ($this->isViewModeCacheable($view_mode) && !$entity->isNew() && $entity->isDefaultRevision() && $this->entityType->isRenderCacheable()) {
182 $build['#cache'] += [
183 'keys' => [
184 'entity_view',
185 $this->entityTypeId,
186 $entity->id(),
187 $view_mode,
188 ],
189 'bin' => $this->cacheBin,
190 ];
191
192 if ($entity instanceof TranslatableInterface && count($entity->getTranslationLanguages()) > 1) {
193 $build['#cache']['keys'][] = $entity->language()->getId();
194 }
195 }
196
197 return $build;
198 }
199
200 /**
201 * Builds an entity's view; augments entity defaults.
202 *
203 * This function is assigned as a #pre_render callback in ::view().
204 *
205 * It transforms the renderable array for a single entity to the same
206 * structure as if we were rendering multiple entities, and then calls the
207 * default ::buildMultiple() #pre_render callback.
208 *
209 * @param array $build
210 * A renderable array containing build information and context for an entity
211 * view.
212 *
213 * @return array
214 * The updated renderable array.
215 *
216 * @see drupal_render()
217 */
218 public function build(array $build) {
219 $build_list = [$build];
220 $build_list = $this->buildMultiple($build_list);
221 return $build_list[0];
222 }
223
224 /**
225 * Builds multiple entities' views; augments entity defaults.
226 *
227 * This function is assigned as a #pre_render callback in ::viewMultiple().
228 *
229 * By delaying the building of an entity until the #pre_render processing in
230 * drupal_render(), the processing cost of assembling an entity's renderable
231 * array is saved on cache-hit requests.
232 *
233 * @param array $build_list
234 * A renderable array containing build information and context for an
235 * entity view.
236 *
237 * @return array
238 * The updated renderable array.
239 *
240 * @see drupal_render()
241 */
242 public function buildMultiple(array $build_list) {
243 // Build the view modes and display objects.
244 $view_modes = [];
245 $entity_type_key = "#{$this->entityTypeId}";
246 $view_hook = "{$this->entityTypeId}_view";
247
248 // Find the keys for the ContentEntities in the build; Store entities for
249 // rendering by view_mode.
250 $children = Element::children($build_list);
251 foreach ($children as $key) {
252 if (isset($build_list[$key][$entity_type_key])) {
253 $entity = $build_list[$key][$entity_type_key];
254 if ($entity instanceof FieldableEntityInterface) {
255 $view_modes[$build_list[$key]['#view_mode']][$key] = $entity;
256 }
257 }
258 }
259
260 // Build content for the displays represented by the entities.
261 foreach ($view_modes as $view_mode => $view_mode_entities) {
262 $displays = EntityViewDisplay::collectRenderDisplays($view_mode_entities, $view_mode);
263 $this->buildComponents($build_list, $view_mode_entities, $displays, $view_mode);
264 foreach (array_keys($view_mode_entities) as $key) {
265 // Allow for alterations while building, before rendering.
266 $entity = $build_list[$key][$entity_type_key];
267 $display = $displays[$entity->bundle()];
268
269 $this->moduleHandler()->invokeAll($view_hook, [&$build_list[$key], $entity, $display, $view_mode]);
270 $this->moduleHandler()->invokeAll('entity_view', [&$build_list[$key], $entity, $display, $view_mode]);
271
272 $this->alterBuild($build_list[$key], $entity, $display, $view_mode);
273
274 // Assign the weights configured in the display.
275 // @todo: Once https://www.drupal.org/node/1875974 provides the missing
276 // API, only do it for 'extra fields', since other components have
277 // been taken care of in EntityViewDisplay::buildMultiple().
278 foreach ($display->getComponents() as $name => $options) {
279 if (isset($build_list[$key][$name])) {
280 $build_list[$key][$name]['#weight'] = $options['weight'];
281 }
282 }
283
284 // Allow modules to modify the render array.
285 $this->moduleHandler()->alter([$view_hook, 'entity_view'], $build_list[$key], $entity, $display);
286 }
287 }
288
289 return $build_list;
290 }
291
292 /**
293 * {@inheritdoc}
294 */
295 public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
296 $entities_by_bundle = [];
297 foreach ($entities as $id => $entity) {
298 // Initialize the field item attributes for the fields being displayed.
299 // The entity can include fields that are not displayed, and the display
300 // can include components that are not fields, so we want to act on the
301 // intersection. However, the entity can have many more fields than are
302 // displayed, so we avoid the cost of calling $entity->getProperties()
303 // by iterating the intersection as follows.
304 foreach ($displays[$entity->bundle()]->getComponents() as $name => $options) {
305 if ($entity->hasField($name)) {
306 foreach ($entity->get($name) as $item) {
307 $item->_attributes = [];
308 }
309 }
310 }
311 // Group the entities by bundle.
312 $entities_by_bundle[$entity->bundle()][$id] = $entity;
313 }
314
315 // Invoke hook_entity_prepare_view().
316 $this->moduleHandler()->invokeAll('entity_prepare_view', [$this->entityTypeId, $entities, $displays, $view_mode]);
317
318 // Let the displays build their render arrays.
319 foreach ($entities_by_bundle as $bundle => $bundle_entities) {
320 $display_build = $displays[$bundle]->buildMultiple($bundle_entities);
321 foreach ($bundle_entities as $id => $entity) {
322 $build[$id] += $display_build[$id];
323 }
324 }
325 }
326
327 /**
328 * Specific per-entity building.
329 *
330 * @param array $build
331 * The render array that is being created.
332 * @param \Drupal\Core\Entity\EntityInterface $entity
333 * The entity to be prepared.
334 * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
335 * The entity view display holding the display options configured for the
336 * entity components.
337 * @param string $view_mode
338 * The view mode that should be used to prepare the entity.
339 */
340 protected function alterBuild(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {}
341
342 /**
343 * {@inheritdoc}
344 */
345 public function getCacheTags() {
346 return [$this->entityTypeId . '_view'];
347 }
348
349 /**
350 * {@inheritdoc}
351 */
352 public function resetCache(array $entities = NULL) {
353 // If no set of specific entities is provided, invalidate the entity view
354 // builder's cache tag. This will invalidate all entities rendered by this
355 // view builder.
356 // Otherwise, if a set of specific entities is provided, invalidate those
357 // specific entities only, plus their list cache tags, because any lists in
358 // which these entities are rendered, must be invalidated as well. However,
359 // even in this case, we might invalidate more cache items than necessary.
360 // When we have a way to invalidate only those cache items that have both
361 // the individual entity's cache tag and the view builder's cache tag, we'll
362 // be able to optimize this further.
363 if (isset($entities)) {
364 $tags = [];
365 foreach ($entities as $entity) {
366 $tags = Cache::mergeTags($tags, $entity->getCacheTags());
367 $tags = Cache::mergeTags($tags, $entity->getEntityType()->getListCacheTags());
368 }
369 Cache::invalidateTags($tags);
370 }
371 else {
372 Cache::invalidateTags($this->getCacheTags());
373 }
374 }
375
376 /**
377 * Determines whether the view mode is cacheable.
378 *
379 * @param string $view_mode
380 * Name of the view mode that should be rendered.
381 *
382 * @return bool
383 * TRUE if the view mode can be cached, FALSE otherwise.
384 */
385 protected function isViewModeCacheable($view_mode) {
386 if ($view_mode == 'default') {
387 // The 'default' is not an actual view mode.
388 return TRUE;
389 }
390 $view_modes_info = $this->entityManager->getViewModes($this->entityTypeId);
391 return !empty($view_modes_info[$view_mode]['cache']);
392 }
393
394 /**
395 * {@inheritdoc}
396 */
397 public function viewField(FieldItemListInterface $items, $display_options = []) {
398 $entity = $items->getEntity();
399 $field_name = $items->getFieldDefinition()->getName();
400 $display = $this->getSingleFieldDisplay($entity, $field_name, $display_options);
401
402 $output = [];
403 $build = $display->build($entity);
404 if (isset($build[$field_name])) {
405 $output = $build[$field_name];
406 }
407
408 return $output;
409 }
410
411 /**
412 * {@inheritdoc}
413 */
414 public function viewFieldItem(FieldItemInterface $item, $display = []) {
415 $entity = $item->getEntity();
416 $field_name = $item->getFieldDefinition()->getName();
417
418 // Clone the entity since we are going to modify field values.
419 $clone = clone $entity;
420
421 // Push the item as the single value for the field, and defer to viewField()
422 // to build the render array for the whole list.
423 $clone->{$field_name}->setValue([$item->getValue()]);
424 $elements = $this->viewField($clone->{$field_name}, $display);
425
426 // Extract the part of the render array we need.
427 $output = isset($elements[0]) ? $elements[0] : [];
428 if (isset($elements['#access'])) {
429 $output['#access'] = $elements['#access'];
430 }
431
432 return $output;
433 }
434
435 /**
436 * Gets an EntityViewDisplay for rendering an individual field.
437 *
438 * @param \Drupal\Core\Entity\EntityInterface $entity
439 * The entity.
440 * @param string $field_name
441 * The field name.
442 * @param string|array $display_options
443 * The display options passed to the viewField() method.
444 *
445 * @return \Drupal\Core\Entity\Display\EntityViewDisplayInterface
446 */
447 protected function getSingleFieldDisplay($entity, $field_name, $display_options) {
448 if (is_string($display_options)) {
449 // View mode: use the Display configured for the view mode.
450 $view_mode = $display_options;
451 $display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
452 // Hide all fields except the current one.
453 foreach (array_keys($entity->getFieldDefinitions()) as $name) {
454 if ($name != $field_name) {
455 $display->removeComponent($name);
456 }
457 }
458 }
459 else {
460 // Array of custom display options: use a runtime Display for the
461 // '_custom' view mode. Persist the displays created, to reduce the number
462 // of objects (displays and formatter plugins) created when rendering a
463 // series of fields individually for cases such as views tables.
464 $entity_type_id = $entity->getEntityTypeId();
465 $bundle = $entity->bundle();
466 $key = $entity_type_id . ':' . $bundle . ':' . $field_name . ':' . Crypt::hashBase64(serialize($display_options));
467 if (!isset($this->singleFieldDisplays[$key])) {
468 $this->singleFieldDisplays[$key] = EntityViewDisplay::create([
469 'targetEntityType' => $entity_type_id,
470 'bundle' => $bundle,
471 'status' => TRUE,
472 ])->setComponent($field_name, $display_options);
473 }
474 $display = $this->singleFieldDisplays[$key];
475 }
476
477 return $display;
478 }
479
480 }