class EntityViewController

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

Defines a generic controller to render a single entity.

Hierarchy

Expanded class hierarchy of EntityViewController

2 files declare their use of EntityViewController
NodePreviewController.php in core/modules/node/src/Controller/NodePreviewController.php
NodeViewController.php in core/modules/node/src/Controller/NodeViewController.php

File

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

Namespace

Drupal\Core\Entity\Controller
View source
class EntityViewController implements ContainerInjectionInterface, TrustedCallbackInterface {
    
    /**
     * The entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * The renderer service.
     *
     * @var \Drupal\Core\Render\RendererInterface
     */
    protected $renderer;
    
    /**
     * Creates an EntityViewController object.
     *
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager.
     * @param \Drupal\Core\Render\RendererInterface $renderer
     *   The renderer service.
     */
    public function __construct(EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer) {
        $this->entityTypeManager = $entity_type_manager;
        $this->renderer = $renderer;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('entity_type.manager'), $container->get('renderer'));
    }
    
    /**
     * Pre-render callback to build the page title.
     *
     * There are two possibilities, depending on the value of the additional
     * entity type property 'enable_page_title_template'.
     * - FALSE (default): use the output of the related field formatter if it
     *   exists. This approach only works correctly for the node entity type and
     *   with the 'string' formatter. In other cases it likely produces illegal
     *   markup and possibly incorrect display. This option has been retained for
     *   backward-compatibility to support sites that expect attributes set on
     *   the field to propagate to the page title.
     * - TRUE: use the output from the entity_page_title template. This approach
     *   works correctly in all cases, without relying on a particular field
     *   formatter or special templates and is the preferred option for the
     *   future.
     *
     * @param array $page
     *   A page render array.
     *
     * @return array
     *   The changed page render array.
     */
    public function buildTitle(array $page) {
        $entity_type = $page['#entity_type'];
        $entity = $page['#' . $entity_type];
        // If the entity has a label field, build the page title based on it.
        if ($entity instanceof FieldableEntityInterface) {
            $label_field = $entity->getEntityType()
                ->getKey('label');
            $template_enabled = $entity->getEntityType()
                ->get('enable_page_title_template');
            if ($label_field && $template_enabled) {
                // Set page title to the output from the entity_page_title template.
                $page_title = [
                    '#theme' => 'entity_page_title',
                    '#title' => $entity->label(),
                    '#entity' => $entity,
                    '#view_mode' => $page['#view_mode'],
                ];
                $page['#title'] = $this->renderer
                    ->render($page_title);
                // Prevent output of the label field in the main content.
                $page[$label_field]['#access'] = FALSE;
                return $page;
            }
            // Set page title to the rendered title field formatter instead of
            // the default plain text title.
            //
            // @todo https://www.drupal.org/project/drupal/issues/3015623
            //   Eventually delete this code and always use the first approach.
            if (isset($page[$label_field])) {
                // Allow templates and theme functions to generate different markup
                // for the page title, which must be inline markup as it will be placed
                // inside <h1>.  See field--node--title.html.twig.
                $page[$label_field]['#is_page_title'] = TRUE;
                $page['#title'] = $this->renderer
                    ->render($page[$label_field]);
            }
        }
        return $page;
    }
    
    /**
     * Provides a page to render a single entity.
     *
     * @param \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.
     * @param string $view_mode
     *   (optional) The view mode that should be used to display the entity.
     *   Defaults to 'full'.
     *
     * @return array
     *   A render array as expected by
     *   \Drupal\Core\Render\RendererInterface::render().
     */
    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;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function trustedCallbacks() {
        return [
            'buildTitle',
        ];
    }
    
    /**
     * Provides a page to render a single entity revision.
     *
     * @param \Drupal\Core\Entity\EntityInterface $_entity_revision
     *   The Entity to be rendered. Note this variable is named $_entity_revision
     *   rather than $entity to prevent collisions with other named placeholders
     *   in the route.
     * @param string $view_mode
     *   (optional) The view mode that should be used to display the entity.
     *   Defaults to 'full'.
     *
     * @return array
     *   A render array.
     *
     * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use
     *   \Drupal\Core\Entity\Controller\EntityRevisionViewController instead.
     *
     * @see https://www.drupal.org/node/3314346
     */
    public function viewRevision(EntityInterface $_entity_revision, $view_mode = 'full') {
        @trigger_error(__METHOD__ . ' is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use \\Drupal\\Core\\Entity\\Controller\\EntityRevisionViewController instead. See https://www.drupal.org/node/3314346', E_USER_DEPRECATED);
        return $this->view($_entity_revision, $view_mode);
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
EntityViewController::$entityTypeManager protected property The entity type manager.
EntityViewController::$renderer protected property The renderer service.
EntityViewController::buildTitle public function Pre-render callback to build the page title.
EntityViewController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 2
EntityViewController::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
EntityViewController::view public function Provides a page to render a single entity. 2
EntityViewController::viewRevision Deprecated public function Provides a page to render a single entity revision.
EntityViewController::__construct public function Creates an EntityViewController object. 2
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING Deprecated constant Untrusted callbacks trigger E_USER_WARNING errors.

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