annotate core/lib/Drupal/Core/Entity/EntityViewBuilder.php @ 19:fa3358dc1485 tip

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