5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Entity\EntityViewBuilder;
9 * View builder handler for nodes.
11 class NodeViewBuilder extends EntityViewBuilder {
16 public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
17 /** @var \Drupal\node\NodeInterface[] $entities */
18 if (empty($entities)) {
22 parent::buildComponents($build, $entities, $displays, $view_mode);
24 foreach ($entities as $id => $entity) {
25 $bundle = $entity->bundle();
26 $display = $displays[$bundle];
28 if ($display->getComponent('links')) {
29 $build[$id]['links'] = [
31 get_called_class() . '::renderLinks', [
34 $entity->language()->getId(),
35 !empty($entity->in_preview),
36 $entity->isDefaultRevision() ? NULL : $entity->getLoadedRevisionId(),
42 // Add Language field text element to node render array.
43 if ($display->getComponent('langcode')) {
44 $build[$id]['langcode'] = [
46 '#title' => t('Language'),
47 '#markup' => $entity->language()->getName(),
48 '#prefix' => '<div id="field-language-display">',
58 protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
59 $defaults = parent::getBuildDefaults($entity, $view_mode);
61 // Don't cache nodes that are in 'preview' mode.
62 if (isset($defaults['#cache']) && isset($entity->in_preview)) {
63 unset($defaults['#cache']);
70 * #lazy_builder callback; builds a node's links.
72 * @param string $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.
81 * (optional) The identifier of the node revision to be loaded. If none
82 * is provided, the default revision will be loaded.
85 * A renderable array representing the node links.
87 public static function renderLinks($node_entity_id, $view_mode, $langcode, $is_in_preview, $revision_id = NULL) {
89 '#theme' => 'links__node',
90 '#pre_render' => ['drupal_pre_render_links'],
91 '#attributes' => ['class' => ['links', 'inline']],
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);
101 // Allow other modules to alter the node links.
103 'view_mode' => $view_mode,
104 'langcode' => $langcode,
106 \Drupal::moduleHandler()->alter('node_links', $links, $entity, $hook_context);
112 * Build the default links (Read more) for a node.
114 * @param \Drupal\node\NodeInterface $entity
116 * @param string $view_mode
117 * A view mode identifier.
120 * An array that can be processed by drupal_pre_render_links().
122 protected static function buildLinks(NodeInterface $entity, $view_mode) {
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,
133 'url' => $entity->urlInfo(),
134 'language' => $entity->language(),
137 'title' => $node_title_stripped,
143 '#theme' => 'links__node__node',
145 '#attributes' => ['class' => ['links', 'inline']],