EntityViewController.php 4.2 KB

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