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