Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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
92    *   \Drupal\Core\Render\RendererInterface::render().
93    */
94   public function view(EntityInterface $_entity, $view_mode = 'full') {
95     $page = $this->entityManager
96       ->getViewBuilder($_entity->getEntityTypeId())
97       ->view($_entity, $view_mode);
98
99     $page['#pre_render'][] = [$this, 'buildTitle'];
100     $page['#entity_type'] = $_entity->getEntityTypeId();
101     $page['#' . $page['#entity_type']] = $_entity;
102
103     return $page;
104   }
105
106   /**
107    * Provides a page to render a single entity revision.
108    *
109    * @param \Drupal\Core\Entity\EntityInterface $_entity_revision
110    *   The Entity to be rendered. Note this variable is named $_entity_revision
111    *   rather than $entity to prevent collisions with other named placeholders
112    *   in the route.
113    * @param string $view_mode
114    *   (optional) The view mode that should be used to display the entity.
115    *   Defaults to 'full'.
116    *
117    * @return array
118    *   A render array.
119    */
120   public function viewRevision(EntityInterface $_entity_revision, $view_mode = 'full') {
121     return $this->view($_entity_revision, $view_mode);
122   }
123
124 }