diff --git a/bookblock.module b/bookblock.module index 0b74891..a62d3bc 100644 --- a/bookblock.module +++ b/bookblock.module @@ -31,6 +31,10 @@ function bookblock_menu() { */ function bookblock_block_info() { $block = array(); + + // Bookblock main navigation block + $block['bookblock-main-book-nav']['info'] = t('Book block : main navigation'); + $books = book_get_books(); $navigationblocks = variable_get('bookblock_books', array()); if (BOOKBLOCK_DEBUG) { @@ -49,7 +53,14 @@ function bookblock_block_info() { * Implements hook_block_view(). */ function bookblock_block_view($delta = '') { - $block = array(); + $block = array(); + + // Check for the Bookblock main navigation block + if ($delta == 'bookblock-main-book-nav') { + $block = _bookblock_book_block_view(); + return $block; + } + $expansion = variable_get('bookblock_block_options_' . $delta, TRUE); // See if we can use the current node to grab the book information. // Call it $node_info as it may not contain everything a node would. @@ -95,12 +106,25 @@ function bookblock_block_view($delta = '') { * Implements hook_block_configure(). */ function bookblock_block_configure($delta) { + $form = array(); + + // We don't need anything to configure for the Bookblock main navigation block + if ($delta == 'bookblock-main-book-nav') { + return $form; + } + $form['bookblock_block_options'] = array( '#type' => 'checkbox', '#title' => t('Allow menu to expand'), '#default_value' => variable_get('bookblock_block_options_' . $delta, TRUE), '#description' => t("If checked, the menu will expand to show child pages as the user navigates through the book. Otherwise it will only show the first level of links. You may not want the menu to expand in the footer, for example."), ); + $form['bookblock_block_options_exclude'] = array( + '#type' => 'checkbox', + '#title' => t('Exclude book from the main book navigation block'), + '#default_value' => variable_get('bookblock_block_options_exclude_' . $delta, FALSE), + '#description' => t("If checked, this book will be excluded from the main book navigation block."), + ); return $form; } @@ -108,7 +132,10 @@ function bookblock_block_configure($delta) { * Implements hook_block_save(). */ function bookblock_block_save($delta, $edit) { - variable_set('bookblock_block_options_' . $delta, $edit['bookblock_block_options']); + if ($delta != 'bookblock-main-book-nav') { + variable_set('bookblock_block_options_' . $delta, $edit['bookblock_block_options']); + variable_set('bookblock_block_options_exclude_' . $delta, $edit['bookblock_block_options_exclude']); + } } /** @@ -153,3 +180,67 @@ function bookblock_book_info_load($nid) { function _bookblock_fix_tree(&$tree) { // dpm($tree); } + +/** + * Custom implementation of book_block_view() to filter books that need to be + * excluded from the main Book navigation block. + * + * This function is copied verbatim from the block module and only an exclusion + * check has been added. + */ +function _bookblock_book_block_view() { + $block = array(); + $current_bid = 0; + if ($node = menu_get_object()) { + $current_bid = empty($node->book['bid']) ? 0 : $node->book['bid']; + } + + if (variable_get('book_block_mode', 'all pages') == 'all pages') { + $block['subject'] = t('Book main navigation'); + $book_menus = array(); + $pseudo_tree = array(0 => array('below' => FALSE)); + foreach (book_get_books() as $book_id => $book) { + // Check if the book has to be excluded + if (variable_get('bookblock_block_options_exclude_' . $book_id, FALSE)) { + // Skip this book + continue; + } + + if ($book['bid'] == $current_bid) { + // If the current page is a node associated with a book, the menu + // needs to be retrieved. + $book_menus[$book_id] = menu_tree_output(menu_tree_all_data($node->book['menu_name'], $node->book)); + } + 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 = node_load($book['nid']); + $book['access'] = node_access('view', $book_node); + $pseudo_tree[0]['link'] = $book; + $book_menus[$book_id] = menu_tree_output($pseudo_tree); + } + } + $book_menus['#theme'] = 'book_all_books_block'; + $block['content'] = $book_menus; + } + elseif ($current_bid) { + // Only display this block when the user is browsing a book. + $select = db_select('node', 'n') + ->fields('n', array('title')) + ->condition('n.nid', $node->book['bid']) + ->addTag('node_access'); + $title = $select->execute()->fetchField(); + // Only show the block if the user has view access for the top-level node. + if ($title) { + $tree = menu_tree_all_data($node->book['menu_name'], $node->book); + // There should only be one element at the top level. + $data = array_shift($tree); + $block['subject'] = theme('book_title_link', array('link' => $data['link'])); + $block['content'] = ($data['below']) ? menu_tree_output($data['below']) : ''; + } + } + + return $block; +}