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;
11 * View builder handler for nodes.
13 class NodeViewBuilder extends EntityViewBuilder {
18 public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
19 /** @var \Drupal\node\NodeInterface[] $entities */
20 if (empty($entities)) {
24 parent::buildComponents($build, $entities, $displays, $view_mode);
26 foreach ($entities as $id => $entity) {
27 $bundle = $entity->bundle();
28 $display = $displays[$bundle];
30 if ($display->getComponent('links')) {
31 $build[$id]['links'] = [
33 get_called_class() . '::renderLinks', [
36 $entity->language()->getId(),
37 !empty($entity->in_preview),
43 // Add Language field text element to node render array.
44 if ($display->getComponent('langcode')) {
45 $build[$id]['langcode'] = [
47 '#title' => t('Language'),
48 '#markup' => $entity->language()->getName(),
49 '#prefix' => '<div id="field-language-display">',
59 protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
60 $defaults = parent::getBuildDefaults($entity, $view_mode);
62 // Don't cache nodes that are in 'preview' mode.
63 if (isset($defaults['#cache']) && isset($entity->in_preview)) {
64 unset($defaults['#cache']);
71 * #lazy_builder callback; builds a node's links.
73 * @param string $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.
83 * A renderable array representing the node links.
85 public static function renderLinks($node_entity_id, $view_mode, $langcode, $is_in_preview) {
87 '#theme' => 'links__node',
88 '#pre_render' => ['drupal_pre_render_links'],
89 '#attributes' => ['class' => ['links', 'inline']],
92 if (!$is_in_preview) {
93 $entity = Node::load($node_entity_id)->getTranslation($langcode);
94 $links['node'] = static::buildLinks($entity, $view_mode);
96 // Allow other modules to alter the node links.
98 'view_mode' => $view_mode,
99 'langcode' => $langcode,
101 \Drupal::moduleHandler()->alter('node_links', $links, $entity, $hook_context);
107 * Build the default links (Read more) for a node.
109 * @param \Drupal\node\NodeInterface $entity
111 * @param string $view_mode
112 * A view mode identifier.
115 * An array that can be processed by drupal_pre_render_links().
117 protected static function buildLinks(NodeInterface $entity, $view_mode) {
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,
128 'url' => $entity->urlInfo(),
129 'language' => $entity->language(),
132 'title' => $node_title_stripped,
138 '#theme' => 'links__node__node',
140 '#attributes' => ['class' => ['links', 'inline']],
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);
151 if ($entity->isDefaultRevision()) {
152 $build['#contextual_links']['node'] = [
153 'route_parameters' => ['node' => $entity->id()],
154 'metadata' => ['changed' => $entity->getChangedTime()],
158 $build['#contextual_links']['node_revision'] = [
159 'route_parameters' => [
160 'node' => $entity->id(),
161 'node_revision' => $entity->getRevisionId(),
163 'metadata' => ['changed' => $entity->getChangedTime()],