You are here

public function FlippyBlock::build in Flippy 8

Implements \Drupal\block\BlockBase::build().

Overrides BlockPluginInterface::build

File

src/Plugin/Block/FlippyBlock.php, line 83

Class

FlippyBlock
Provides a "Flippy" block.

Namespace

Drupal\flippy\Plugin\Block

Code

public function build() {
  $build = [];

  // Detect if we're viewing a node.
  if ($node = $this->request->attributes
    ->get('node')) {

    // Make sure this node type is still enabled.
    if ($this->flippyPager
      ->flippy_use_pager($node)) {
      $build = [
        '#theme' => 'flippy',
        '#list' => $this->flippyPager
          ->flippy_build_list($node),
        '#node' => $node,
        '#attached' => [
          'library' => [
            'flippy/drupal.flippy',
          ],
        ],
      ];

      // Set head elements.
      if (is_object($node)) {
        if ($this->flippySettings
          ->get('flippy_head_' . $node
          ->getType())) {
          $links = $this->flippyPager
            ->flippy_build_list($node);
          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(),
            ];
          }
        }
      }
    }
  }
  return $build;
}