/** * {@inheritdoc} */ public function build() { $current_bid = 0; if ($node = $this->requestStack->getCurrentRequest()->get('node')) { $current_bid = empty($node->book['bid']) ? 0 : $node->book['bid']; } if ($this->configuration['block_mode'] == 'all pages') { $book_menus = array(); $pseudo_tree = array(0 => array('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] += array('#book_title' => $book['title']); } if ($book_menus) { return array('#theme' => 'book_all_books_block') + $book_menus; } } elseif ($current_bid) { // Only display this block when the user is browsing a book. $query = \Drupal::entityQuery('node'); $nid = $query->condition('nid', $node->book['bid'], '=')->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 array(); }
/** * Formats the book links for the child pages of the current page. * * @param array $book_link * A fully loaded book link that is part of the book hierarchy. * * @return array * HTML for the links to the child pages of the current page. */ public function childrenLinks(array $book_link) { $flat = $this->bookManager->bookTreeGetFlat($book_link); $children = array(); if ($book_link['has_children']) { // Walk through the array until we find the current page. do { $link = array_shift($flat); } while ($link && $link['nid'] != $book_link['nid']); // Continue though the array and collect the links whose parent is this page. while (($link = array_shift($flat)) && $link['pid'] == $book_link['nid']) { $data['link'] = $link; $data['below'] = ''; $children[] = $data; } } if ($children) { return $this->bookManager->bookTreeOutput($children); } return ''; }