function EntityViewBuilder::getBuildDefaults

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/EntityViewBuilder.php \Drupal\Core\Entity\EntityViewBuilder::getBuildDefaults()
  2. 10 core/lib/Drupal/Core/Entity/EntityViewBuilder.php \Drupal\Core\Entity\EntityViewBuilder::getBuildDefaults()
  3. 11.x core/lib/Drupal/Core/Entity/EntityViewBuilder.php \Drupal\Core\Entity\EntityViewBuilder::getBuildDefaults()

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

5 calls to EntityViewBuilder::getBuildDefaults()
BlockContentViewBuilder::getBuildDefaults in core/modules/block_content/src/BlockContentViewBuilder.php
Provides entity-specific defaults to the build process.
CommentViewBuilder::getBuildDefaults in core/modules/comment/src/CommentViewBuilder.php
Provides entity-specific defaults to the build process.
EntityViewBuilder::viewMultiple in core/lib/Drupal/Core/Entity/EntityViewBuilder.php
Builds the render array for the provided entities.
MessageViewBuilder::getBuildDefaults in core/modules/contact/src/MessageViewBuilder.php
Provides entity-specific defaults to the build process.
NodeViewBuilder::getBuildDefaults in core/modules/node/src/NodeViewBuilder.php
Provides entity-specific defaults to the build process.
4 methods override EntityViewBuilder::getBuildDefaults()
BlockContentViewBuilder::getBuildDefaults in core/modules/block_content/src/BlockContentViewBuilder.php
Provides entity-specific defaults to the build process.
CommentViewBuilder::getBuildDefaults in core/modules/comment/src/CommentViewBuilder.php
Provides entity-specific defaults to the build process.
MessageViewBuilder::getBuildDefaults in core/modules/contact/src/MessageViewBuilder.php
Provides entity-specific defaults to the build process.
NodeViewBuilder::getBuildDefaults in core/modules/node/src/NodeViewBuilder.php
Provides entity-specific defaults to the build process.

File

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

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 = [
        "#{$this->entityTypeId}" => $entity,
        '#view_mode' => $view_mode,
        // Collect cache defaults for this entity.
'#cache' => [
            'tags' => Cache::mergeTags($this->getCacheTags(), $entity->getCacheTags()),
            'contexts' => $entity->getCacheContexts(),
            'max-age' => $entity->getCacheMaxAge(),
        ],
    ];
    // Add the default #theme key if a template exists for it.
    if ($this->themeRegistry
        ->getRuntime()
        ->has($this->entityTypeId)) {
        $build['#theme'] = $this->entityTypeId;
    }
    // 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'] += [
            'keys' => [
                'entity_view',
                $this->entityTypeId,
                $entity->id(),
                $view_mode,
            ],
            'bin' => $this->cacheBin,
        ];
        if ($entity instanceof TranslatableDataInterface && count($entity->getTranslationLanguages()) > 1) {
            $build['#cache']['keys'][] = $entity->language()
                ->getId();
        }
    }
    return $build;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.