You are here

function book_block in Drupal 6

Same name and namespace in other branches
  1. 4 modules/book.module \book_block()
  2. 5 modules/book/book.module \book_block()

Implementation of hook_block().

Displays the book table of contents in a block when the current page is a single-node view of a book node.

File

modules/book/book.module, line 175
Allows users to structure the pages of a site in a hierarchy or outline.

Code

function book_block($op = 'list', $delta = 0, $edit = array()) {
  $block = array();
  switch ($op) {
    case 'list':
      $block[0]['info'] = t('Book navigation');
      $block[0]['cache'] = BLOCK_CACHE_PER_PAGE | BLOCK_CACHE_PER_ROLE;
      return $block;
    case 'view':
      $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 navigation');
        $book_menus = array();
        $pseudo_tree = array(
          0 => array(
            'below' => FALSE,
          ),
        );
        foreach (book_get_books() 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.
            $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;
            $pseudo_tree[0]['link'] = $book;
            $book_menus[$book_id] = menu_tree_output($pseudo_tree);
          }
        }
        $block['content'] = theme('book_all_books_block', $book_menus);
      }
      elseif ($current_bid) {

        // Only display this block when the user is browsing a book.
        $title = db_result(db_query(db_rewrite_sql('SELECT n.title FROM {node} n WHERE n.nid = %d'), $node->book['bid']));

        // 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', $data['link']);
          $block['content'] = $data['below'] ? menu_tree_output($data['below']) : '';
        }
      }
      return $block;
    case 'configure':
      $options = array(
        'all pages' => t('Show block on all pages'),
        'book pages' => t('Show block only on book pages'),
      );
      $form['book_block_mode'] = array(
        '#type' => 'radios',
        '#title' => t('Book navigation block display'),
        '#options' => $options,
        '#default_value' => variable_get('book_block_mode', 'all pages'),
        '#description' => t("If <em>Show block on all pages</em> is selected, the block will contain the automatically generated menus for all of the site's books. If <em>Show block only on book pages</em> is selected, the block will contain only the one menu corresponding to the current page's book. In this case, if the current page is not in a book, no block will be displayed. The <em>Page specific visibility settings</em> or other visibility settings can be used in addition to selectively display this block."),
      );
      return $form;
    case 'save':
      variable_set('book_block_mode', $edit['book_block_mode']);
      break;
  }
}