NodeViewBuilder.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace Drupal\node;
  3. use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
  4. use Drupal\Core\Entity\EntityInterface;
  5. use Drupal\Core\Entity\EntityViewBuilder;
  6. use Drupal\node\Entity\Node;
  7. /**
  8. * View builder handler for nodes.
  9. */
  10. class NodeViewBuilder extends EntityViewBuilder {
  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. ],
  32. ],
  33. ];
  34. }
  35. // Add Language field text element to node render array.
  36. if ($display->getComponent('langcode')) {
  37. $build[$id]['langcode'] = [
  38. '#type' => 'item',
  39. '#title' => t('Language'),
  40. '#markup' => $entity->language()->getName(),
  41. '#prefix' => '<div id="field-language-display">',
  42. '#suffix' => '</div>'
  43. ];
  44. }
  45. }
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
  51. $defaults = parent::getBuildDefaults($entity, $view_mode);
  52. // Don't cache nodes that are in 'preview' mode.
  53. if (isset($defaults['#cache']) && isset($entity->in_preview)) {
  54. unset($defaults['#cache']);
  55. }
  56. return $defaults;
  57. }
  58. /**
  59. * #lazy_builder callback; builds a node's links.
  60. *
  61. * @param string $node_entity_id
  62. * The node entity ID.
  63. * @param string $view_mode
  64. * The view mode in which the node entity is being viewed.
  65. * @param string $langcode
  66. * The language in which the node entity is being viewed.
  67. * @param bool $is_in_preview
  68. * Whether the node is currently being previewed.
  69. *
  70. * @return array
  71. * A renderable array representing the node links.
  72. */
  73. public static function renderLinks($node_entity_id, $view_mode, $langcode, $is_in_preview) {
  74. $links = [
  75. '#theme' => 'links__node',
  76. '#pre_render' => ['drupal_pre_render_links'],
  77. '#attributes' => ['class' => ['links', 'inline']],
  78. ];
  79. if (!$is_in_preview) {
  80. $entity = Node::load($node_entity_id)->getTranslation($langcode);
  81. $links['node'] = static::buildLinks($entity, $view_mode);
  82. // Allow other modules to alter the node links.
  83. $hook_context = [
  84. 'view_mode' => $view_mode,
  85. 'langcode' => $langcode,
  86. ];
  87. \Drupal::moduleHandler()->alter('node_links', $links, $entity, $hook_context);
  88. }
  89. return $links;
  90. }
  91. /**
  92. * Build the default links (Read more) for a node.
  93. *
  94. * @param \Drupal\node\NodeInterface $entity
  95. * The node object.
  96. * @param string $view_mode
  97. * A view mode identifier.
  98. *
  99. * @return array
  100. * An array that can be processed by drupal_pre_render_links().
  101. */
  102. protected static function buildLinks(NodeInterface $entity, $view_mode) {
  103. $links = [];
  104. // Always display a read more link on teasers because we have no way
  105. // to know when a teaser view is different than a full view.
  106. if ($view_mode == 'teaser') {
  107. $node_title_stripped = strip_tags($entity->label());
  108. $links['node-readmore'] = [
  109. 'title' => t('Read more<span class="visually-hidden"> about @title</span>', [
  110. '@title' => $node_title_stripped,
  111. ]),
  112. 'url' => $entity->urlInfo(),
  113. 'language' => $entity->language(),
  114. 'attributes' => [
  115. 'rel' => 'tag',
  116. 'title' => $node_title_stripped,
  117. ],
  118. ];
  119. }
  120. return [
  121. '#theme' => 'links__node__node',
  122. '#links' => $links,
  123. '#attributes' => ['class' => ['links', 'inline']],
  124. ];
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. protected function alterBuild(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  130. /** @var \Drupal\node\NodeInterface $entity */
  131. parent::alterBuild($build, $entity, $display, $view_mode);
  132. if ($entity->id()) {
  133. if ($entity->isDefaultRevision()) {
  134. $build['#contextual_links']['node'] = [
  135. 'route_parameters' => ['node' => $entity->id()],
  136. 'metadata' => ['changed' => $entity->getChangedTime()],
  137. ];
  138. }
  139. else {
  140. $build['#contextual_links']['node_revision'] = [
  141. 'route_parameters' => [
  142. 'node' => $entity->id(),
  143. 'node_revision' => $entity->getRevisionId(),
  144. ],
  145. 'metadata' => ['changed' => $entity->getChangedTime()],
  146. ];
  147. }
  148. }
  149. }
  150. }