EntityViewController.php 3.18 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\Core\Entity\Controller\EntityViewController.
 */

namespace Drupal\Core\Entity\Controller;

10
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
11
use Drupal\Core\Entity\EntityInterface;
12
use Drupal\Core\Entity\FieldableEntityInterface;
13
use Drupal\Core\Entity\EntityManagerInterface;
14
use Drupal\Core\Render\RendererInterface;
15 16 17 18 19
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Defines a generic controller to render a single entity.
 */
20
class EntityViewController implements ContainerInjectionInterface {
21 22 23 24

  /**
   * The entity manager
   *
25
   * @var \Drupal\Core\Entity\EntityManagerInterface
26 27 28
   */
  protected $entityManager;

29 30 31 32 33 34 35
  /**
   * The renderer service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

36
  /**
37
   * Creates an EntityViewController object.
38
   *
39
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
40
   *   The entity manager.
41 42
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer service.
43
   */
44
  public function __construct(EntityManagerInterface $entity_manager, RendererInterface $renderer) {
45
    $this->entityManager = $entity_manager;
46
    $this->renderer = $renderer;
47 48 49 50 51 52 53
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
54 55
      $container->get('entity.manager'),
      $container->get('renderer')
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    );
  }

  /**
   * 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'.
   * @param string $langcode
   *   (optional) For which language the entity should be rendered, defaults to
   *   the current content language.
   *
   * @return array
   *   A render array as expected by drupal_render().
   */
  public function view(EntityInterface $_entity, $view_mode = 'full', $langcode = NULL) {
77
    $page = $this->entityManager
78
      ->getViewBuilder($_entity->getEntityTypeId())
79
      ->view($_entity, $view_mode, $langcode);
80 81 82 83 84

    // If the entity's label is rendered using a field formatter, set the
    // rendered title field formatter as the page title instead of the default
    // plain text title. This allows attributes set on the field to propagate
    // correctly (e.g. RDFa, in-place editing).
85
    if ($_entity instanceof FieldableEntityInterface) {
86 87 88 89 90 91 92 93
      $label_field = $_entity->getEntityType()->getKey('label');
      if ($label_field && $_entity->getFieldDefinition($label_field)->getDisplayOptions('view')) {
        // We must render the label field, because rendering the entity may be
        // a cache hit, in which case we can't extract the rendered label field
        // from the $page renderable array.
        $build = $this->entityManager->getTranslationFromContext($_entity)
          ->get($label_field)
          ->view($view_mode);
94
        $page['#title'] = $this->renderer->render($build);
95 96 97 98
      }
    }

    return $page;
99 100 101
  }

}