You are here

function flippy_node_view in Flippy 8

Same name and namespace in other branches
  1. 7 flippy.module \flippy_node_view()

Implements hook_ENTITY_TYPE_view() for node entities.

File

./flippy.module, line 374
Allows administrators to add previous/next pagers to any node type.

Code

function flippy_node_view(array &$build, EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) {

  // Only add the pager if it should be used for this node's content type.
  if (\Drupal::service('flippy.pager')
    ->flippy_use_pager($node) && $display
    ->getComponent('flippy_pager')) {
    $links = \Drupal::service('flippy.pager')
      ->flippy_build_list($node);
    $build['flippy_pager'] = [
      '#theme' => 'flippy',
      '#list' => $links,
      '#node' => $node,
      '#attached' => [
        'library' => [
          'flippy/drupal.flippy',
        ],
      ],
      '#cache' => [
        'tags' => [
          'node_list',
        ],
      ],
    ];
    if (is_object($node)) {

      // Check if we need to support HammerJS.
      if (\Drupal::config('flippy.settings')
        ->get('flippy_press_swipe_' . $node
        ->getType())) {
        $build['flippy_pager']['#attached']['library'][] = 'hammerjs/hammerjs';
        $build['flippy_pager']['#attached']['library'][] = 'flippy/flippy.swipe';
      }

      // Add the previous/next elements to the page head, if enable for this
      // content type.
      if (\Drupal::config('flippy.settings')
        ->get('flippy_head_' . $node
        ->getType())) {
        if (!empty($links['prev']['nid'])) {
          $build['#attached']['html_head_link'][][] = [
            'rel' => 'prev',
            'href' => Url::fromRoute('entity.node.canonical', [
              'node' => $links['prev']['nid'],
            ])
              ->toString(),
          ];
        }
        if (!empty($links['next']['nid'])) {
          $build['#attached']['html_head_link'][][] = [
            'rel' => 'next',
            'href' => Url::fromRoute('entity.node.canonical', [
              'node' => $links['next']['nid'],
            ])
              ->toString(),
          ];
        }
      }
    }
  }
}