NodeViewBuilder.php 4.4 KB

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