function bookblock_block in Book Block 6
Implementation of hook_block().
File
- ./
bookblock.module, line 27 - Enables users to add specific book navigation blocks on non-book pages.
Code
function bookblock_block($op = 'list', $delta = 0, $edit = array()) {
$block = array();
$books = book_get_books();
$navigationblocks = variable_get('bookblock_books', array());
switch ($op) {
case 'list':
foreach ($navigationblocks as $navigationblock) {
if ($books[$navigationblock]) {
$block[$navigationblock]['info'] = t('Book block: @title', array(
'@title' => $books[$navigationblock]['title'],
));
$block[$navigationblock]['cache'] = BLOCK_CACHE_PER_PAGE | BLOCK_CACHE_PER_ROLE;
}
}
return $block;
case 'view':
if ($delta) {
$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();
if (!$node_info || $node_info->book['bid'] != $delta || !$expansion) {
// Pull in the minimum we need to get book info and node access working.
$node_info = db_fetch_object(db_query('SELECT n.nid, n.uid, n.status FROM {node} n WHERE n.nid = %d', $delta));
$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);
}
// 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']) {
$data = array_shift(menu_tree_all_data($node_info->book['menu_name'], $node_info->book));
}
$block['subject'] = theme('book_title_link', $data['link']);
if ($data['below']) {
$block['content'] = menu_tree_output($data['below']);
}
}
}
return $block;
case 'configure':
$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."),
);
return $form;
case 'save':
variable_set('bookblock_block_options_' . $delta, $edit['bookblock_block_options']);
break;
}
}