You are here

function prevnext_node_view in Prevnext 2.x

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

Implements hook_ENTITY_TYPE_view().

File

./prevnext.module, line 84
Contains prevnext.module.

Code

function prevnext_node_view(array &$build, Node $node, EntityViewDisplayInterface $display, $view_mode) {

  // Checking if current node is configured for prevnext or not.
  $config = \Drupal::config('prevnext.settings');
  $enabled_nodetypes = $config
    ->get('prevnext_enabled_nodetypes');
  if (empty($enabled_nodetypes[$node
    ->getType()])) {
    return;
  }

  /** @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();
}