You are here

public function PrevNextBlock::build in Previous/Next API 8.2

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/PrevNextBlock.php, line 157

Class

PrevNextBlock
Provides a 'Previous Next' block.

Namespace

Drupal\prev_next\Plugin\Block

Code

public function build() {
  $build = [
    '#theme' => 'prev_next_block',
  ];

  /* @var $node \Drupal\node\NodeInterface */
  $node = $this
    ->getContextValue('node');
  if ($node instanceof NodeInterface) {
    $prev_id = $this->prevnextHelper
      ->getPrevnextId($node
      ->id(), 'prev');
    $next_id = $this->prevnextHelper
      ->getPrevnextId($node
      ->id(), 'next');
    if ($next_id || $prev_id) {
      if ($prev_id && $this->configuration['prev_display'] && $this->configuration['prev_text'] != '') {
        $build += [
          '#prev_display' => $this->configuration['prev_display'],
          '#prev_text' => $this->configuration['prev_text'],
          '#prev_id' => $prev_id,
        ];
      }
      if ($next_id && $this->configuration['next_display'] && $this->configuration['next_text'] != '') {
        $build += [
          '#next_display' => $this->configuration['next_display'],
          '#next_text' => $this->configuration['next_text'],
          '#next_id' => $next_id,
        ];
      }
    }
  }
  return $build;
}