Chris@0: 'entity.manager']; Chris@0: Chris@0: /** Chris@0: * The type of entities for which this view builder is instantiated. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: protected $entityTypeId; Chris@0: Chris@0: /** Chris@0: * Information about the entity type. Chris@0: * Chris@0: * @var \Drupal\Core\Entity\EntityTypeInterface Chris@0: */ Chris@0: protected $entityType; Chris@0: Chris@0: /** Chris@18: * The entity repository service. Chris@0: * Chris@18: * @var \Drupal\Core\Entity\EntityRepositoryInterface Chris@0: */ Chris@18: protected $entityRepository; Chris@18: Chris@18: /** Chris@18: * The entity display repository. Chris@18: * Chris@18: * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface Chris@18: */ Chris@18: protected $entityDisplayRepository; Chris@0: Chris@0: /** Chris@0: * The cache bin used to store the render cache. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: protected $cacheBin = 'render'; Chris@0: Chris@0: /** Chris@0: * The language manager. Chris@0: * Chris@12: * @var \Drupal\Core\Language\LanguageManagerInterface Chris@0: */ Chris@0: protected $languageManager; Chris@0: Chris@0: /** Chris@0: * The theme registry. Chris@0: * Chris@0: * @var \Drupal\Core\Theme\Registry Chris@0: */ Chris@0: protected $themeRegistry; Chris@0: Chris@0: /** Chris@0: * The EntityViewDisplay objects created for individual field rendering. Chris@0: * Chris@12: * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface[] Chris@12: * Chris@0: * @see \Drupal\Core\Entity\EntityViewBuilder::getSingleFieldDisplay() Chris@0: */ Chris@0: protected $singleFieldDisplays; Chris@0: Chris@0: /** Chris@0: * Constructs a new EntityViewBuilder. Chris@0: * Chris@0: * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type Chris@0: * The entity type definition. Chris@18: * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository Chris@18: * The entity repository service. Chris@0: * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager Chris@0: * The language manager. Chris@0: * @param \Drupal\Core\Theme\Registry $theme_registry Chris@0: * The theme registry. Chris@18: * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository Chris@18: * The entity display repository. Chris@0: */ Chris@18: public function __construct(EntityTypeInterface $entity_type, EntityRepositoryInterface $entity_repository, LanguageManagerInterface $language_manager, Registry $theme_registry = NULL, EntityDisplayRepositoryInterface $entity_display_repository = NULL) { Chris@0: $this->entityTypeId = $entity_type->id(); Chris@0: $this->entityType = $entity_type; Chris@18: $this->entityRepository = $entity_repository; Chris@0: $this->languageManager = $language_manager; Chris@0: $this->themeRegistry = $theme_registry ?: \Drupal::service('theme.registry'); Chris@18: if (!$entity_display_repository) { Chris@18: @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: $entity_display_repository = \Drupal::service('entity_display.repository'); Chris@18: } Chris@18: $this->entityDisplayRepository = $entity_display_repository; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { Chris@0: return new static( Chris@0: $entity_type, Chris@18: $container->get('entity.repository'), Chris@0: $container->get('language_manager'), Chris@18: $container->get('theme.registry'), Chris@18: $container->get('entity_display.repository') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) { Chris@0: $build_list = $this->viewMultiple([$entity], $view_mode, $langcode); Chris@0: Chris@0: // The default ::buildMultiple() #pre_render callback won't run, because we Chris@0: // extract a child element of the default renderable array. Thus we must Chris@0: // assign an alternative #pre_render callback that applies the necessary Chris@0: // transformations and then still calls ::buildMultiple(). Chris@0: $build = $build_list[0]; Chris@0: $build['#pre_render'][] = [$this, 'build']; Chris@0: Chris@0: return $build; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function viewMultiple(array $entities = [], $view_mode = 'full', $langcode = NULL) { Chris@0: $build_list = [ Chris@0: '#sorted' => TRUE, Chris@0: '#pre_render' => [[$this, 'buildMultiple']], Chris@0: ]; Chris@0: $weight = 0; Chris@0: foreach ($entities as $key => $entity) { Chris@0: // Ensure that from now on we are dealing with the proper translation Chris@0: // object. Chris@18: $entity = $this->entityRepository->getTranslationFromContext($entity, $langcode); Chris@0: Chris@0: // Set build defaults. Chris@0: $build_list[$key] = $this->getBuildDefaults($entity, $view_mode); Chris@0: $entityType = $this->entityTypeId; Chris@0: $this->moduleHandler()->alter([$entityType . '_build_defaults', 'entity_build_defaults'], $build_list[$key], $entity, $view_mode); Chris@0: Chris@0: $build_list[$key]['#weight'] = $weight++; Chris@0: } Chris@0: Chris@0: return $build_list; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Provides entity-specific defaults to the build process. Chris@0: * Chris@0: * @param \Drupal\Core\Entity\EntityInterface $entity Chris@0: * The entity for which the defaults should be provided. Chris@0: * @param string $view_mode Chris@0: * The view mode that should be used. Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: protected function getBuildDefaults(EntityInterface $entity, $view_mode) { Chris@0: // Allow modules to change the view mode. Chris@0: $context = []; Chris@0: $this->moduleHandler()->alter('entity_view_mode', $view_mode, $entity, $context); Chris@0: Chris@0: $build = [ Chris@0: "#{$this->entityTypeId}" => $entity, Chris@0: '#view_mode' => $view_mode, Chris@0: // Collect cache defaults for this entity. Chris@0: '#cache' => [ Chris@0: 'tags' => Cache::mergeTags($this->getCacheTags(), $entity->getCacheTags()), Chris@0: 'contexts' => $entity->getCacheContexts(), Chris@0: 'max-age' => $entity->getCacheMaxAge(), Chris@0: ], Chris@0: ]; Chris@0: Chris@0: // Add the default #theme key if a template exists for it. Chris@0: if ($this->themeRegistry->getRuntime()->has($this->entityTypeId)) { Chris@0: $build['#theme'] = $this->entityTypeId; Chris@0: } Chris@0: Chris@0: // Cache the rendered output if permitted by the view mode and global entity Chris@0: // type configuration. Chris@0: if ($this->isViewModeCacheable($view_mode) && !$entity->isNew() && $entity->isDefaultRevision() && $this->entityType->isRenderCacheable()) { Chris@0: $build['#cache'] += [ Chris@0: 'keys' => [ Chris@0: 'entity_view', Chris@0: $this->entityTypeId, Chris@0: $entity->id(), Chris@0: $view_mode, Chris@0: ], Chris@0: 'bin' => $this->cacheBin, Chris@0: ]; Chris@0: Chris@14: if ($entity instanceof TranslatableDataInterface && count($entity->getTranslationLanguages()) > 1) { Chris@0: $build['#cache']['keys'][] = $entity->language()->getId(); Chris@0: } Chris@0: } Chris@0: Chris@0: return $build; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Builds an entity's view; augments entity defaults. Chris@0: * Chris@0: * This function is assigned as a #pre_render callback in ::view(). Chris@0: * Chris@0: * It transforms the renderable array for a single entity to the same Chris@0: * structure as if we were rendering multiple entities, and then calls the Chris@0: * default ::buildMultiple() #pre_render callback. Chris@0: * Chris@0: * @param array $build Chris@0: * A renderable array containing build information and context for an entity Chris@0: * view. Chris@0: * Chris@0: * @return array Chris@0: * The updated renderable array. Chris@0: * Chris@16: * @see \Drupal\Core\Render\RendererInterface::render() Chris@0: */ Chris@0: public function build(array $build) { Chris@0: $build_list = [$build]; Chris@0: $build_list = $this->buildMultiple($build_list); Chris@0: return $build_list[0]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Builds multiple entities' views; augments entity defaults. Chris@0: * Chris@0: * This function is assigned as a #pre_render callback in ::viewMultiple(). Chris@0: * Chris@0: * By delaying the building of an entity until the #pre_render processing in Chris@0: * drupal_render(), the processing cost of assembling an entity's renderable Chris@0: * array is saved on cache-hit requests. Chris@0: * Chris@0: * @param array $build_list Chris@0: * A renderable array containing build information and context for an Chris@0: * entity view. Chris@0: * Chris@0: * @return array Chris@0: * The updated renderable array. Chris@0: * Chris@16: * @see \Drupal\Core\Render\RendererInterface::render() Chris@0: */ Chris@0: public function buildMultiple(array $build_list) { Chris@0: // Build the view modes and display objects. Chris@0: $view_modes = []; Chris@0: $entity_type_key = "#{$this->entityTypeId}"; Chris@0: $view_hook = "{$this->entityTypeId}_view"; Chris@0: Chris@0: // Find the keys for the ContentEntities in the build; Store entities for Chris@0: // rendering by view_mode. Chris@0: $children = Element::children($build_list); Chris@0: foreach ($children as $key) { Chris@0: if (isset($build_list[$key][$entity_type_key])) { Chris@0: $entity = $build_list[$key][$entity_type_key]; Chris@0: if ($entity instanceof FieldableEntityInterface) { Chris@0: $view_modes[$build_list[$key]['#view_mode']][$key] = $entity; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: // Build content for the displays represented by the entities. Chris@0: foreach ($view_modes as $view_mode => $view_mode_entities) { Chris@0: $displays = EntityViewDisplay::collectRenderDisplays($view_mode_entities, $view_mode); Chris@0: $this->buildComponents($build_list, $view_mode_entities, $displays, $view_mode); Chris@0: foreach (array_keys($view_mode_entities) as $key) { Chris@0: // Allow for alterations while building, before rendering. Chris@0: $entity = $build_list[$key][$entity_type_key]; Chris@0: $display = $displays[$entity->bundle()]; Chris@0: Chris@0: $this->moduleHandler()->invokeAll($view_hook, [&$build_list[$key], $entity, $display, $view_mode]); Chris@0: $this->moduleHandler()->invokeAll('entity_view', [&$build_list[$key], $entity, $display, $view_mode]); Chris@0: Chris@14: $this->addContextualLinks($build_list[$key], $entity); Chris@0: $this->alterBuild($build_list[$key], $entity, $display, $view_mode); Chris@0: Chris@0: // Assign the weights configured in the display. Chris@0: // @todo: Once https://www.drupal.org/node/1875974 provides the missing Chris@0: // API, only do it for 'extra fields', since other components have Chris@0: // been taken care of in EntityViewDisplay::buildMultiple(). Chris@0: foreach ($display->getComponents() as $name => $options) { Chris@0: if (isset($build_list[$key][$name])) { Chris@0: $build_list[$key][$name]['#weight'] = $options['weight']; Chris@0: } Chris@0: } Chris@0: Chris@0: // Allow modules to modify the render array. Chris@0: $this->moduleHandler()->alter([$view_hook, 'entity_view'], $build_list[$key], $entity, $display); Chris@0: } Chris@0: } Chris@0: Chris@0: return $build_list; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function buildComponents(array &$build, array $entities, array $displays, $view_mode) { Chris@0: $entities_by_bundle = []; Chris@0: foreach ($entities as $id => $entity) { Chris@0: // Initialize the field item attributes for the fields being displayed. Chris@0: // The entity can include fields that are not displayed, and the display Chris@0: // can include components that are not fields, so we want to act on the Chris@0: // intersection. However, the entity can have many more fields than are Chris@0: // displayed, so we avoid the cost of calling $entity->getProperties() Chris@0: // by iterating the intersection as follows. Chris@0: foreach ($displays[$entity->bundle()]->getComponents() as $name => $options) { Chris@0: if ($entity->hasField($name)) { Chris@0: foreach ($entity->get($name) as $item) { Chris@0: $item->_attributes = []; Chris@0: } Chris@0: } Chris@0: } Chris@0: // Group the entities by bundle. Chris@0: $entities_by_bundle[$entity->bundle()][$id] = $entity; Chris@0: } Chris@0: Chris@0: // Invoke hook_entity_prepare_view(). Chris@0: $this->moduleHandler()->invokeAll('entity_prepare_view', [$this->entityTypeId, $entities, $displays, $view_mode]); Chris@0: Chris@0: // Let the displays build their render arrays. Chris@0: foreach ($entities_by_bundle as $bundle => $bundle_entities) { Chris@0: $display_build = $displays[$bundle]->buildMultiple($bundle_entities); Chris@0: foreach ($bundle_entities as $id => $entity) { Chris@0: $build[$id] += $display_build[$id]; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@14: * Add contextual links. Chris@14: * Chris@14: * @param array $build Chris@14: * The render array that is being created. Chris@14: * @param \Drupal\Core\Entity\EntityInterface $entity Chris@14: * The entity to be prepared. Chris@14: */ Chris@14: protected function addContextualLinks(array &$build, EntityInterface $entity) { Chris@14: if ($entity->isNew()) { Chris@14: return; Chris@14: } Chris@14: $key = $entity->getEntityTypeId(); Chris@14: $rel = 'canonical'; Chris@14: if ($entity instanceof ContentEntityInterface && !$entity->isDefaultRevision()) { Chris@14: $rel = 'revision'; Chris@14: $key .= '_revision'; Chris@14: } Chris@14: if ($entity->hasLinkTemplate($rel)) { Chris@14: $build['#contextual_links'][$key] = [ Chris@14: 'route_parameters' => $entity->toUrl($rel)->getRouteParameters(), Chris@14: ]; Chris@14: if ($entity instanceof EntityChangedInterface) { Chris@14: $build['#contextual_links'][$key]['metadata'] = [ Chris@14: 'changed' => $entity->getChangedTime(), Chris@14: ]; Chris@14: } Chris@14: } Chris@14: } Chris@14: Chris@14: /** Chris@0: * Specific per-entity building. Chris@0: * Chris@0: * @param array $build Chris@0: * The render array that is being created. Chris@0: * @param \Drupal\Core\Entity\EntityInterface $entity Chris@0: * The entity to be prepared. Chris@0: * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display Chris@0: * The entity view display holding the display options configured for the Chris@0: * entity components. Chris@0: * @param string $view_mode Chris@0: * The view mode that should be used to prepare the entity. Chris@0: */ Chris@0: protected function alterBuild(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {} Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getCacheTags() { Chris@0: return [$this->entityTypeId . '_view']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function resetCache(array $entities = NULL) { Chris@0: // If no set of specific entities is provided, invalidate the entity view Chris@0: // builder's cache tag. This will invalidate all entities rendered by this Chris@0: // view builder. Chris@0: // Otherwise, if a set of specific entities is provided, invalidate those Chris@0: // specific entities only, plus their list cache tags, because any lists in Chris@0: // which these entities are rendered, must be invalidated as well. However, Chris@0: // even in this case, we might invalidate more cache items than necessary. Chris@0: // When we have a way to invalidate only those cache items that have both Chris@0: // the individual entity's cache tag and the view builder's cache tag, we'll Chris@0: // be able to optimize this further. Chris@0: if (isset($entities)) { Chris@0: $tags = []; Chris@0: foreach ($entities as $entity) { Chris@0: $tags = Cache::mergeTags($tags, $entity->getCacheTags()); Chris@0: $tags = Cache::mergeTags($tags, $entity->getEntityType()->getListCacheTags()); Chris@0: } Chris@0: Cache::invalidateTags($tags); Chris@0: } Chris@0: else { Chris@0: Cache::invalidateTags($this->getCacheTags()); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Determines whether the view mode is cacheable. Chris@0: * Chris@0: * @param string $view_mode Chris@0: * Name of the view mode that should be rendered. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if the view mode can be cached, FALSE otherwise. Chris@0: */ Chris@0: protected function isViewModeCacheable($view_mode) { Chris@0: if ($view_mode == 'default') { Chris@0: // The 'default' is not an actual view mode. Chris@0: return TRUE; Chris@0: } Chris@18: $view_modes_info = $this->entityDisplayRepository->getViewModes($this->entityTypeId); Chris@0: return !empty($view_modes_info[$view_mode]['cache']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function viewField(FieldItemListInterface $items, $display_options = []) { Chris@0: $entity = $items->getEntity(); Chris@0: $field_name = $items->getFieldDefinition()->getName(); Chris@0: $display = $this->getSingleFieldDisplay($entity, $field_name, $display_options); Chris@0: Chris@0: $output = []; Chris@0: $build = $display->build($entity); Chris@0: if (isset($build[$field_name])) { Chris@0: $output = $build[$field_name]; Chris@0: } Chris@0: Chris@0: return $output; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function viewFieldItem(FieldItemInterface $item, $display = []) { Chris@0: $entity = $item->getEntity(); Chris@0: $field_name = $item->getFieldDefinition()->getName(); Chris@0: Chris@0: // Clone the entity since we are going to modify field values. Chris@0: $clone = clone $entity; Chris@0: Chris@0: // Push the item as the single value for the field, and defer to viewField() Chris@0: // to build the render array for the whole list. Chris@0: $clone->{$field_name}->setValue([$item->getValue()]); Chris@0: $elements = $this->viewField($clone->{$field_name}, $display); Chris@0: Chris@0: // Extract the part of the render array we need. Chris@0: $output = isset($elements[0]) ? $elements[0] : []; Chris@0: if (isset($elements['#access'])) { Chris@0: $output['#access'] = $elements['#access']; Chris@0: } Chris@0: Chris@0: return $output; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets an EntityViewDisplay for rendering an individual field. Chris@0: * Chris@0: * @param \Drupal\Core\Entity\EntityInterface $entity Chris@0: * The entity. Chris@0: * @param string $field_name Chris@0: * The field name. Chris@0: * @param string|array $display_options Chris@0: * The display options passed to the viewField() method. Chris@0: * Chris@0: * @return \Drupal\Core\Entity\Display\EntityViewDisplayInterface Chris@0: */ Chris@0: protected function getSingleFieldDisplay($entity, $field_name, $display_options) { Chris@0: if (is_string($display_options)) { Chris@0: // View mode: use the Display configured for the view mode. Chris@0: $view_mode = $display_options; Chris@0: $display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode); Chris@0: // Hide all fields except the current one. Chris@0: foreach (array_keys($entity->getFieldDefinitions()) as $name) { Chris@0: if ($name != $field_name) { Chris@0: $display->removeComponent($name); Chris@0: } Chris@0: } Chris@0: } Chris@0: else { Chris@0: // Array of custom display options: use a runtime Display for the Chris@0: // '_custom' view mode. Persist the displays created, to reduce the number Chris@0: // of objects (displays and formatter plugins) created when rendering a Chris@0: // series of fields individually for cases such as views tables. Chris@0: $entity_type_id = $entity->getEntityTypeId(); Chris@0: $bundle = $entity->bundle(); Chris@0: $key = $entity_type_id . ':' . $bundle . ':' . $field_name . ':' . Crypt::hashBase64(serialize($display_options)); Chris@0: if (!isset($this->singleFieldDisplays[$key])) { Chris@0: $this->singleFieldDisplays[$key] = EntityViewDisplay::create([ Chris@0: 'targetEntityType' => $entity_type_id, Chris@0: 'bundle' => $bundle, Chris@0: 'status' => TRUE, Chris@0: ])->setComponent($field_name, $display_options); Chris@0: } Chris@0: $display = $this->singleFieldDisplays[$key]; Chris@0: } Chris@0: Chris@0: return $display; Chris@0: } Chris@0: Chris@0: }