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