EntityViewController.php 3.7 KB

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