983eec85fea69e4644bd3992687feb654d49a68b
[yaffs-website] / web / core / lib / Drupal / Core / Entity / Controller / EntityViewController.php
1 <?php
2
3 namespace Drupal\Core\Entity\Controller;
4
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\FieldableEntityInterface;
8 use Drupal\Core\Entity\EntityManagerInterface;
9 use Drupal\Core\Render\RendererInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Defines a generic controller to render a single entity.
14  */
15 class EntityViewController implements ContainerInjectionInterface {
16
17   /**
18    * The entity manager
19    *
20    * @var \Drupal\Core\Entity\EntityManagerInterface
21    */
22   protected $entityManager;
23
24   /**
25    * The renderer service.
26    *
27    * @var \Drupal\Core\Render\RendererInterface
28    */
29   protected $renderer;
30
31   /**
32    * Creates an EntityViewController object.
33    *
34    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
35    *   The entity manager.
36    * @param \Drupal\Core\Render\RendererInterface $renderer
37    *   The renderer service.
38    */
39   public function __construct(EntityManagerInterface $entity_manager, RendererInterface $renderer) {
40     $this->entityManager = $entity_manager;
41     $this->renderer = $renderer;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public static function create(ContainerInterface $container) {
48     return new static(
49       $container->get('entity.manager'),
50       $container->get('renderer')
51     );
52   }
53
54   /**
55    * Pre-render callback to build the page title.
56    *
57    * @param array $page
58    *   A page render array.
59    *
60    * @return array
61    *   The changed page render array.
62    */
63   public function buildTitle(array $page) {
64     $entity_type = $page['#entity_type'];
65     $entity = $page['#' . $entity_type];
66     // If the entity's label is rendered using a field formatter, set the
67     // rendered title field formatter as the page title instead of the default
68     // plain text title. This allows attributes set on the field to propagate
69     // correctly (e.g. RDFa, in-place editing).
70     if ($entity instanceof FieldableEntityInterface) {
71       $label_field = $entity->getEntityType()->getKey('label');
72       if (isset($page[$label_field])) {
73         $page['#title'] = $this->renderer->render($page[$label_field]);
74       }
75     }
76     return $page;
77   }
78
79   /**
80    * Provides a page to render a single entity.
81    *
82    * @param \Drupal\Core\Entity\EntityInterface $_entity
83    *   The Entity to be rendered. Note this variable is named $_entity rather
84    *   than $entity to prevent collisions with other named placeholders in the
85    *   route.
86    * @param string $view_mode
87    *   (optional) The view mode that should be used to display the entity.
88    *   Defaults to 'full'.
89    *
90    * @return array
91    *   A render array as expected by drupal_render().
92    */
93   public function view(EntityInterface $_entity, $view_mode = 'full') {
94     $page = $this->entityManager
95       ->getViewBuilder($_entity->getEntityTypeId())
96       ->view($_entity, $view_mode);
97
98     $page['#pre_render'][] = [$this, 'buildTitle'];
99     $page['#entity_type'] = $_entity->getEntityTypeId();
100     $page['#' . $page['#entity_type']] = $_entity;
101
102     return $page;
103   }
104
105   /**
106    * Provides a page to render a single entity revision.
107    *
108    * @param \Drupal\Core\Entity\EntityInterface $_entity_revision
109    *   The Entity to be rendered. Note this variable is named $_entity_revision
110    *   rather than $entity to prevent collisions with other named placeholders
111    *   in the route.
112    * @param string $view_mode
113    *   (optional) The view mode that should be used to display the entity.
114    *   Defaults to 'full'.
115    *
116    * @return array
117    *   A render array.
118    */
119   public function viewRevision(EntityInterface $_entity_revision, $view_mode = 'full') {
120     return $this->view($_entity_revision, $view_mode);
121   }
122
123 }