function EntityViewController::view

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/Controller/EntityViewController.php \Drupal\Core\Entity\Controller\EntityViewController::view()
  2. 8.9.x core/lib/Drupal/Core/Entity/Controller/EntityViewController.php \Drupal\Core\Entity\Controller\EntityViewController::view()
  3. 11.x core/lib/Drupal/Core/Entity/Controller/EntityViewController.php \Drupal\Core\Entity\Controller\EntityViewController::view()

Provides a page to render a single entity.

Parameters

\Drupal\Core\Entity\EntityInterface $_entity: The Entity to be rendered. Note this variable is named $_entity rather than $entity to prevent collisions with other named placeholders in the route.

string $view_mode: (optional) The view mode that should be used to display the entity. Defaults to 'full'.

Return value

array A render array as expected by \Drupal\Core\Render\RendererInterface::render().

3 calls to EntityViewController::view()
EntityViewController::viewRevision in core/lib/Drupal/Core/Entity/Controller/EntityViewController.php
Provides a page to render a single entity revision.
NodePreviewController::view in core/modules/node/src/Controller/NodePreviewController.php
Provides a page to render a single entity.
NodeViewController::view in core/modules/node/src/Controller/NodeViewController.php
Provides a page to render a single entity.
2 methods override EntityViewController::view()
NodePreviewController::view in core/modules/node/src/Controller/NodePreviewController.php
Provides a page to render a single entity.
NodeViewController::view in core/modules/node/src/Controller/NodeViewController.php
Provides a page to render a single entity.

File

core/lib/Drupal/Core/Entity/Controller/EntityViewController.php, line 131

Class

EntityViewController
Defines a generic controller to render a single entity.

Namespace

Drupal\Core\Entity\Controller

Code

public function view(EntityInterface $_entity, $view_mode = 'full') {
    $page = $this->entityTypeManager
        ->getViewBuilder($_entity->getEntityTypeId())
        ->view($_entity, $view_mode);
    $page['#pre_render'][] = [
        $this,
        'buildTitle',
    ];
    $page['#entity_type'] = $_entity->getEntityTypeId();
    $page['#' . $page['#entity_type']] = $_entity;
    // Add canonical and shortlink links if the entity has a canonical
    // link template and is not new.
    if ($_entity->hasLinkTemplate('canonical') && !$_entity->isNew()) {
        $url = $_entity->toUrl('canonical')
            ->setAbsolute(TRUE);
        $page['#attached']['html_head_link'][] = [
            [
                'rel' => 'canonical',
                'href' => $url->toString(),
            ],
        ];
        // Set the non-aliased canonical path as a default shortlink.
        $page['#attached']['html_head_link'][] = [
            [
                'rel' => 'shortlink',
                'href' => $url->setOption('alias', TRUE)
                    ->toString(),
            ],
        ];
        // Since this generates absolute URLs, it can only be cached "per site".
        $page['#cache']['contexts'][] = 'url.site';
    }
    return $page;
}

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