You are here

private function NextPreviousBlock::generateNextPrevious in Next Previous Post Block (Node or Page Pagination) 9.0.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/Block/NextPreviousBlock.php \Drupal\nextpre\Plugin\Block\NextPreviousBlock::generateNextPrevious()
  2. 8 src/Plugin/Block/NextPreviousBlock.php \Drupal\nextpre\Plugin\Block\NextPreviousBlock::generateNextPrevious()
  3. 1.0.x src/Plugin/Block/NextPreviousBlock.php \Drupal\nextpre\Plugin\Block\NextPreviousBlock::generateNextPrevious()

Lookup the next or previous node.

Parameters

string $node: Get current page node id.

string $direction: Default value is "next" and other value come from generatePrevious() and generatePrevious().

Return value

array Find the alias of the next node.

2 calls to NextPreviousBlock::generateNextPrevious()
NextPreviousBlock::generateNext in src/Plugin/Block/NextPreviousBlock.php
Lookup the next node,oldest node which is still younger than the node.
NextPreviousBlock::generatePrevious in src/Plugin/Block/NextPreviousBlock.php
Lookup the previous node,youngest node which is still older than the node.

File

src/Plugin/Block/NextPreviousBlock.php, line 216

Class

NextPreviousBlock
Provides a 'Next Previous' block.

Namespace

Drupal\nextpre\Plugin\Block

Code

private function generateNextPrevious($node, $direction = self::DIRECTION__NEXT) {
  $comparison_opperator = '>';
  $sort = 'ASC';
  $display_text = $this->configuration['next_text'];
  $class = $this->configuration['nextlink_class'] ? $this->configuration['nextlink_class'] : 'btn';
  $current_nid = $node
    ->id();
  $current_langcode = $node
    ->get('langcode')->value;
  if ($direction === 'prev') {
    $comparison_opperator = '<';
    $sort = 'DESC';
    $display_text = $this->configuration['previous_text'];
    $class = $this->configuration['previouslink_class'] ? $this->configuration['previouslink_class'] : 'btn';
  }

  // Lookup 1 node younger (or older) than the current node.
  $query = $this->entityTypeManager
    ->getStorage('node');
  $query_result = $query
    ->getQuery();
  $next = $query_result
    ->condition('nid', $current_nid, $comparison_opperator)
    ->condition('type', $this->configuration['content_type'])
    ->condition('status', 1)
    ->condition('langcode', $current_langcode)
    ->sort('nid', $sort)
    ->range(0, 1)
    ->execute();

  // If this is not the youngest (or oldest) node.
  if (!empty($next) && is_array($next)) {
    $next = array_values($next);
    $next = $next[0];

    // Find the alias of the next node.
    $nid = $next;
    $url = Url::fromRoute('entity.node.canonical', [
      'node' => $nid,
    ], []);
    $link = Link::fromTextAndUrl($display_text, Url::fromUri('internal:/' . $url
      ->getInternalPath()));
    $link = $link
      ->toRenderable();
    $link['#attributes'] = [
      'class' => [
        'nextpre__btn',
        $class,
      ],
    ];
    return $link;
  }
  return '';
}