View source
<?php
function book_explorer_block_info() {
$blocks = array();
$blocks['book_explorer'] = array(
'info' => t('Book explorer navigation'),
'cache' => DRUPAL_CACHE_PER_ROLE | DRUPAL_CACHE_PER_PAGE,
);
return $blocks;
}
function book_explorer_block_configure($delta = '') {
$block = array();
$options = array(
'all pages' => t('Show block on all pages'),
'book pages' => t('Show block only on book pages'),
);
$form['book_explorer_block_mode'] = array(
'#type' => 'radios',
'#title' => t('Book navigation block display'),
'#options' => $options,
'#default_value' => variable_get('book_explorer_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;
}
function book_explorer_block_save($delta = '', $edit = array()) {
$block = array();
variable_set('book_explorer_block_mode', $edit['book_explorer_block_mode']);
}
function book_explorer_block_view($delta = '') {
return book_explorer_block_book_explorer_view();
}
function book_explorer_block_book_explorer_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_explorer_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) {
$book_menus[$book_id] = menu_tree_output(menu_tree_all_data($node->book['menu_name']));
}
else {
$book['in_active_trail'] = FALSE;
$book_node = node_load($book['nid']);
$book['access'] = node_access('view', $book_node);
$book_menus[$book_id] = menu_tree_output(menu_tree_all_data($book_node->book['menu_name']));
}
}
$book_menus['#theme'] = 'book_all_books_block';
$block['content'] = $book_menus;
}
elseif ($current_bid) {
$select = db_select('node', 'n')
->fields('n', array(
'title',
))
->condition('n.nid', $node->book['bid'])
->addTag('node_access');
$title = $select
->execute()
->fetchField();
if ($title) {
$tree = menu_tree_all_data($node->book['menu_name']);
$data = array_shift($tree);
$block['subject'] = theme('book_title_link', array(
'link' => $data['link'],
));
$block['content'] = $data['below'] ? menu_tree_output($data['below']) : '';
}
}
if (!empty($block['content'])) {
$block['content'] = array(
'prefix' => array(
'#markup' => '<div class="book-explorer">',
),
'build' => $block['content'],
'suffix' => array(
'#markup' => '</div>',
),
);
$dir = drupal_get_path('module', 'book_explorer');
$block['content']['build']['#attached']['css'] = array(
$dir . '/book_explorer.css',
);
$block['content']['build']['#attached']['js'] = array(
$dir . '/book_explorer.js',
);
}
return $block;
}