Mercurial > hg > isophonics-drupal-site
comparison core/modules/node/src/NodeViewBuilder.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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' => [ | |
33 get_called_class() . '::renderLinks', [ | |
34 $entity->id(), | |
35 $view_mode, | |
36 $entity->language()->getId(), | |
37 !empty($entity->in_preview), | |
38 ], | |
39 ], | |
40 ]; | |
41 } | |
42 | |
43 // Add Language field text element to node render array. | |
44 if ($display->getComponent('langcode')) { | |
45 $build[$id]['langcode'] = [ | |
46 '#type' => 'item', | |
47 '#title' => t('Language'), | |
48 '#markup' => $entity->language()->getName(), | |
49 '#prefix' => '<div id="field-language-display">', | |
50 '#suffix' => '</div>' | |
51 ]; | |
52 } | |
53 } | |
54 } | |
55 | |
56 /** | |
57 * {@inheritdoc} | |
58 */ | |
59 protected function getBuildDefaults(EntityInterface $entity, $view_mode) { | |
60 $defaults = parent::getBuildDefaults($entity, $view_mode); | |
61 | |
62 // Don't cache nodes that are in 'preview' mode. | |
63 if (isset($defaults['#cache']) && isset($entity->in_preview)) { | |
64 unset($defaults['#cache']); | |
65 } | |
66 | |
67 return $defaults; | |
68 } | |
69 | |
70 /** | |
71 * #lazy_builder callback; builds a node's links. | |
72 * | |
73 * @param string $node_entity_id | |
74 * The node entity ID. | |
75 * @param string $view_mode | |
76 * The view mode in which the node entity is being viewed. | |
77 * @param string $langcode | |
78 * The language in which the node entity is being viewed. | |
79 * @param bool $is_in_preview | |
80 * Whether the node is currently being previewed. | |
81 * | |
82 * @return array | |
83 * A renderable array representing the node links. | |
84 */ | |
85 public static function renderLinks($node_entity_id, $view_mode, $langcode, $is_in_preview) { | |
86 $links = [ | |
87 '#theme' => 'links__node', | |
88 '#pre_render' => ['drupal_pre_render_links'], | |
89 '#attributes' => ['class' => ['links', 'inline']], | |
90 ]; | |
91 | |
92 if (!$is_in_preview) { | |
93 $entity = Node::load($node_entity_id)->getTranslation($langcode); | |
94 $links['node'] = static::buildLinks($entity, $view_mode); | |
95 | |
96 // Allow other modules to alter the node links. | |
97 $hook_context = [ | |
98 'view_mode' => $view_mode, | |
99 'langcode' => $langcode, | |
100 ]; | |
101 \Drupal::moduleHandler()->alter('node_links', $links, $entity, $hook_context); | |
102 } | |
103 return $links; | |
104 } | |
105 | |
106 /** | |
107 * Build the default links (Read more) for a node. | |
108 * | |
109 * @param \Drupal\node\NodeInterface $entity | |
110 * The node object. | |
111 * @param string $view_mode | |
112 * A view mode identifier. | |
113 * | |
114 * @return array | |
115 * An array that can be processed by drupal_pre_render_links(). | |
116 */ | |
117 protected static function buildLinks(NodeInterface $entity, $view_mode) { | |
118 $links = []; | |
119 | |
120 // Always display a read more link on teasers because we have no way | |
121 // to know when a teaser view is different than a full view. | |
122 if ($view_mode == 'teaser') { | |
123 $node_title_stripped = strip_tags($entity->label()); | |
124 $links['node-readmore'] = [ | |
125 'title' => t('Read more<span class="visually-hidden"> about @title</span>', [ | |
126 '@title' => $node_title_stripped, | |
127 ]), | |
128 'url' => $entity->urlInfo(), | |
129 'language' => $entity->language(), | |
130 'attributes' => [ | |
131 'rel' => 'tag', | |
132 'title' => $node_title_stripped, | |
133 ], | |
134 ]; | |
135 } | |
136 | |
137 return [ | |
138 '#theme' => 'links__node__node', | |
139 '#links' => $links, | |
140 '#attributes' => ['class' => ['links', 'inline']], | |
141 ]; | |
142 } | |
143 | |
144 /** | |
145 * {@inheritdoc} | |
146 */ | |
147 protected function alterBuild(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) { | |
148 /** @var \Drupal\node\NodeInterface $entity */ | |
149 parent::alterBuild($build, $entity, $display, $view_mode); | |
150 if ($entity->id()) { | |
151 if ($entity->isDefaultRevision()) { | |
152 $build['#contextual_links']['node'] = [ | |
153 'route_parameters' => ['node' => $entity->id()], | |
154 'metadata' => ['changed' => $entity->getChangedTime()], | |
155 ]; | |
156 } | |
157 else { | |
158 $build['#contextual_links']['node_revision'] = [ | |
159 'route_parameters' => [ | |
160 'node' => $entity->id(), | |
161 'node_revision' => $entity->getRevisionId(), | |
162 ], | |
163 'metadata' => ['changed' => $entity->getChangedTime()], | |
164 ]; | |
165 } | |
166 } | |
167 } | |
168 | |
169 } |