protected function EntityViewBuilder::getBuildDefaults

protected EntityViewBuilder::getBuildDefaults(EntityInterface $entity, $view_mode)

Provides entity-specific defaults to the build process.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity for which the defaults should be provided.

string $view_mode: The view mode that should be used.

Return value

array

File

core/lib/Drupal/Core/Entity/EntityViewBuilder.php, line 145

Class

EntityViewBuilder
Base class for entity view builders.

Namespace

Drupal\Core\Entity

Code

protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
  // Allow modules to change the view mode.
  $context = [];
  $this->moduleHandler()->alter('entity_view_mode', $view_mode, $entity, $context);

  $build = array(
    '#theme' => $this->entityTypeId,
    "#{$this->entityTypeId}" => $entity,
    '#view_mode' => $view_mode,
    // Collect cache defaults for this entity.
    '#cache' => array(
      'tags' => Cache::mergeTags($this->getCacheTags(), $entity->getCacheTags()),
      'contexts' => $entity->getCacheContexts(),
      'max-age' => $entity->getCacheMaxAge(),
    ),
  );

  // Cache the rendered output if permitted by the view mode and global entity
  // type configuration.
  if ($this->isViewModeCacheable($view_mode) && !$entity->isNew() && $entity->isDefaultRevision() && $this->entityType->isRenderCacheable()) {
    $build['#cache'] += array(
      'keys' => array(
        'entity_view',
        $this->entityTypeId,
        $entity->id(),
        $view_mode,
      ),
      'bin' => $this->cacheBin,
    );

    if ($entity instanceof TranslatableInterface && count($entity->getTranslationLanguages()) > 1) {
      $build['#cache']['keys'][] = $entity->language()->getId();
    }
  }

  return $build;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!EntityViewBuilder.php/function/EntityViewBuilder::getBuildDefaults/8.1.x