You are here

public function BookNavigationBlock::build in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/book/src/Plugin/Block/BookNavigationBlock.php \Drupal\book\Plugin\Block\BookNavigationBlock::build()

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

core/modules/book/src/Plugin/Block/BookNavigationBlock.php, line 123

Class

BookNavigationBlock
Provides a 'Book navigation' block.

Namespace

Drupal\book\Plugin\Block

Code

public function build() {
  $current_bid = 0;
  $node = $this->routeMatch
    ->getParameter('node');
  if ($node instanceof NodeInterface && !empty($node->book['bid'])) {
    $current_bid = $node->book['bid'];
  }
  if ($this->configuration['block_mode'] == 'all pages') {
    $book_menus = [];
    $pseudo_tree = [
      0 => [
        'below' => FALSE,
      ],
    ];
    foreach ($this->bookManager
      ->getAllBooks() as $book_id => $book) {
      if ($book['bid'] == $current_bid) {

        // If the current page is a node associated with a book, the menu
        // needs to be retrieved.
        $data = $this->bookManager
          ->bookTreeAllData($node->book['bid'], $node->book);
        $book_menus[$book_id] = $this->bookManager
          ->bookTreeOutput($data);
      }
      else {

        // Since we know we will only display a link to the top node, there
        // is no reason to run an additional menu tree query for each book.
        $book['in_active_trail'] = FALSE;

        // Check whether user can access the book link.
        $book_node = $this->nodeStorage
          ->load($book['nid']);
        $book['access'] = $book_node
          ->access('view');
        $pseudo_tree[0]['link'] = $book;
        $book_menus[$book_id] = $this->bookManager
          ->bookTreeOutput($pseudo_tree);
      }
      $book_menus[$book_id] += [
        '#book_title' => $book['title'],
      ];
    }
    if ($book_menus) {
      return [
        '#theme' => 'book_all_books_block',
      ] + $book_menus;
    }
  }
  elseif ($current_bid) {

    // Only display this block when the user is browsing a book and do
    // not show unpublished books.
    $nid = \Drupal::entityQuery('node')
      ->accessCheck(TRUE)
      ->condition('nid', $node->book['bid'], '=')
      ->condition('status', NodeInterface::PUBLISHED)
      ->execute();

    // Only show the block if the user has view access for the top-level node.
    if ($nid) {
      $tree = $this->bookManager
        ->bookTreeAllData($node->book['bid'], $node->book);

      // There should only be one element at the top level.
      $data = array_shift($tree);
      $below = $this->bookManager
        ->bookTreeOutput($data['below']);
      if (!empty($below)) {
        return $below;
      }
    }
  }
  return [];
}