123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- namespace Drupal\Core\Entity\Controller;
- use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
- use Drupal\Core\Entity\EntityInterface;
- use Drupal\Core\Entity\FieldableEntityInterface;
- use Drupal\Core\Entity\EntityManagerInterface;
- use Drupal\Core\Render\RendererInterface;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- /**
- * Defines a generic controller to render a single entity.
- */
- class EntityViewController implements ContainerInjectionInterface {
- /**
- * The entity manager
- *
- * @var \Drupal\Core\Entity\EntityManagerInterface
- */
- protected $entityManager;
- /**
- * The renderer service.
- *
- * @var \Drupal\Core\Render\RendererInterface
- */
- protected $renderer;
- /**
- * Creates an EntityViewController object.
- *
- * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
- * The entity manager.
- * @param \Drupal\Core\Render\RendererInterface $renderer
- * The renderer service.
- */
- public function __construct(EntityManagerInterface $entity_manager, RendererInterface $renderer) {
- $this->entityManager = $entity_manager;
- $this->renderer = $renderer;
- }
- /**
- * {@inheritdoc}
- */
- public static function create(ContainerInterface $container) {
- return new static(
- $container->get('entity.manager'),
- $container->get('renderer')
- );
- }
- /**
- * Pre-render callback to build the page title.
- *
- * @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'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).
- if ($entity instanceof FieldableEntityInterface) {
- $label_field = $entity->getEntityType()->getKey('label');
- if (isset($page[$label_field])) {
- $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_render().
- */
- public function view(EntityInterface $_entity, $view_mode = 'full') {
- $page = $this->entityManager
- ->getViewBuilder($_entity->getEntityTypeId())
- ->view($_entity, $view_mode);
- $page['#pre_render'][] = [$this, 'buildTitle'];
- $page['#entity_type'] = $_entity->getEntityTypeId();
- $page['#' . $page['#entity_type']] = $_entity;
- return $page;
- }
- /**
- * 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.
- */
- public function viewRevision(EntityInterface $_entity_revision, $view_mode = 'full') {
- return $this->view($_entity_revision, $view_mode);
- }
- }
|