You are here

function prevnext_node_view in Prevnext 8

Same name and namespace in other branches
  1. 7 prevnext.module \prevnext_node_view()
  2. 2.x prevnext.module \prevnext_node_view()
  3. 2.0.x prevnext.module \prevnext_node_view()

Implements hook_ENTITY_TYPE_view().

File

./prevnext.module, line 81
Contains prevnext.module.

Code

function prevnext_node_view(array &$build, \Drupal\node\Entity\Node $node, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {

  /** @var \Drupal\prevnext\PrevnextService $prevnext */
  $prevnext = \Drupal::service('prevnext.service');
  $previous_next = $prevnext
    ->getPreviousNext($node);
  if ($display
    ->getComponent('prevnext_previous')) {
    $build['prevnext_previous'] = [
      '#theme' => 'prevnext',
      '#direction' => 'previous',
      '#text' => t('Previous'),
      '#nid' => $previous_next['prev'],
      '#url' => Url::fromUserInput('/node/' . $previous_next['prev'])
        ->toString(),
      '#void' => empty($previous_next['prev']),
    ];
  }
  if ($display
    ->getComponent('prevnext_next')) {
    $build['prevnext_next'] = [
      '#theme' => 'prevnext',
      '#direction' => 'next',
      '#text' => t('Next'),
      '#nid' => $previous_next['next'],
      '#url' => Url::fromUserInput('/node/' . $previous_next['next'])
        ->toString(),
      '#void' => empty($previous_next['next']),
    ];
  }

  // Once these links will be cached inside the node rendered output, we will
  // add a custom cache tag to allow invalidation of all these cached info
  // later (for example when a new node of this type is created).
  $build['#cache']['tags'][] = 'prevnext-' . $node
    ->bundle();
}