You are here

function bookblock_block_view in Book Block 7

Implements hook_block_view().

File

./bookblock.module, line 51
Enables users to add specific book navigation blocks on non-book pages.

Code

function bookblock_block_view($delta = '') {
  $block = array();
  $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.
  $node_info = menu_get_object();
  $bid = NULL;
  if (isset($node_info->book['bid'])) {
    $bid = $node_info->book['bid'];
  }
  if (!$node_info || $bid != $delta || !$expansion) {

    // Pull in the minimum we need to get book info and node access working.
    $node_info = db_query('SELECT n.nid, n.uid, n.status, n.type FROM {node} n WHERE n.nid = :nid', array(
      ':nid' => $delta,
    ))
      ->fetchObject();
    $node_info->book = bookblock_book_info_load($delta);
  }

  // Now see if we can render something.
  if (node_access('view', $node_info) && isset($node_info->book)) {
    if ($expansion) {
      $tree = menu_tree_page_data($node_info->book['menu_name']);
    }
    else {

      // The problem with this is there will be no active trail.
      // I have a feeling something custom is going to have to be written.
      $tree = menu_tree_all_data($node_info->book['menu_name'], $node_info->book);
      _bookblock_fix_tree($tree);
    }

    // There should only be one element at the top level.
    $data = array_shift($tree);

    // Check we've got some data if we've used menu_tree_page_data.
    // Doesn't appear to have been an opportunity to do this before.
    if ($expansion && !$data['below']) {
      $tree = menu_tree_all_data($node_info->book['menu_name'], $node_info->book);
      $data = array_shift($tree);
    }
    $block['subject'] = theme('book_title_link', array(
      'link' => $data['link'],
    ));
    if ($data['below']) {
      $block['content'] = menu_tree_output($data['below']);
    }
  }
  return $block;
}