NodeViewBuilder.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace Drupal\node;
  3. use Drupal\Core\Entity\EntityInterface;
  4. use Drupal\Core\Entity\EntityViewBuilder;
  5. use Drupal\Core\Render\Element\Link;
  6. use Drupal\Core\Security\TrustedCallbackInterface;
  7. /**
  8. * View builder handler for nodes.
  9. */
  10. class NodeViewBuilder extends EntityViewBuilder implements TrustedCallbackInterface {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
  15. /** @var \Drupal\node\NodeInterface[] $entities */
  16. if (empty($entities)) {
  17. return;
  18. }
  19. parent::buildComponents($build, $entities, $displays, $view_mode);
  20. foreach ($entities as $id => $entity) {
  21. $bundle = $entity->bundle();
  22. $display = $displays[$bundle];
  23. if ($display->getComponent('links')) {
  24. $build[$id]['links'] = [
  25. '#lazy_builder' => [
  26. get_called_class() . '::renderLinks', [
  27. $entity->id(),
  28. $view_mode,
  29. $entity->language()->getId(),
  30. !empty($entity->in_preview),
  31. $entity->isDefaultRevision() ? NULL : $entity->getLoadedRevisionId(),
  32. ],
  33. ],
  34. ];
  35. }
  36. // Add Language field text element to node render array.
  37. if ($display->getComponent('langcode')) {
  38. $build[$id]['langcode'] = [
  39. '#type' => 'item',
  40. '#title' => t('Language'),
  41. '#markup' => $entity->language()->getName(),
  42. '#prefix' => '<div id="field-language-display">',
  43. '#suffix' => '</div>',
  44. ];
  45. }
  46. }
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
  52. $defaults = parent::getBuildDefaults($entity, $view_mode);
  53. // Don't cache nodes that are in 'preview' mode.
  54. if (isset($defaults['#cache']) && isset($entity->in_preview)) {
  55. unset($defaults['#cache']);
  56. }
  57. return $defaults;
  58. }
  59. /**
  60. * #lazy_builder callback; builds a node's links.
  61. *
  62. * @param string $node_entity_id
  63. * The node entity ID.
  64. * @param string $view_mode
  65. * The view mode in which the node entity is being viewed.
  66. * @param string $langcode
  67. * The language in which the node entity is being viewed.
  68. * @param bool $is_in_preview
  69. * Whether the node is currently being previewed.
  70. * @param $revision_id
  71. * (optional) The identifier of the node revision to be loaded. If none
  72. * is provided, the default revision will be loaded.
  73. *
  74. * @return array
  75. * A renderable array representing the node links.
  76. */
  77. public static function renderLinks($node_entity_id, $view_mode, $langcode, $is_in_preview, $revision_id = NULL) {
  78. $links = [
  79. '#theme' => 'links__node',
  80. '#pre_render' => [[Link::class, 'preRenderLinks']],
  81. '#attributes' => ['class' => ['links', 'inline']],
  82. ];
  83. if (!$is_in_preview) {
  84. $storage = \Drupal::entityTypeManager()->getStorage('node');
  85. /** @var \Drupal\node\NodeInterface $revision */
  86. $revision = !isset($revision_id) ? $storage->load($node_entity_id) : $storage->loadRevision($revision_id);
  87. $entity = $revision->getTranslation($langcode);
  88. $links['node'] = static::buildLinks($entity, $view_mode);
  89. // Allow other modules to alter the node links.
  90. $hook_context = [
  91. 'view_mode' => $view_mode,
  92. 'langcode' => $langcode,
  93. ];
  94. \Drupal::moduleHandler()->alter('node_links', $links, $entity, $hook_context);
  95. }
  96. return $links;
  97. }
  98. /**
  99. * Build the default links (Read more) for a node.
  100. *
  101. * @param \Drupal\node\NodeInterface $entity
  102. * The node object.
  103. * @param string $view_mode
  104. * A view mode identifier.
  105. *
  106. * @return array
  107. * An array that can be processed by drupal_pre_render_links().
  108. */
  109. protected static function buildLinks(NodeInterface $entity, $view_mode) {
  110. $links = [];
  111. // Always display a read more link on teasers because we have no way
  112. // to know when a teaser view is different than a full view.
  113. if ($view_mode == 'teaser') {
  114. $node_title_stripped = strip_tags($entity->label());
  115. $links['node-readmore'] = [
  116. 'title' => t('Read more<span class="visually-hidden"> about @title</span>', [
  117. '@title' => $node_title_stripped,
  118. ]),
  119. 'url' => $entity->toUrl(),
  120. 'language' => $entity->language(),
  121. 'attributes' => [
  122. 'rel' => 'tag',
  123. 'title' => $node_title_stripped,
  124. ],
  125. ];
  126. }
  127. return [
  128. '#theme' => 'links__node__node',
  129. '#links' => $links,
  130. '#attributes' => ['class' => ['links', 'inline']],
  131. ];
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public static function trustedCallbacks() {
  137. $callbacks = parent::trustedCallbacks();
  138. $callbacks[] = 'renderLinks';
  139. return $callbacks;
  140. }
  141. }