#164348 by pwolanin: improve book block performance

6.x
Gábor Hojtsy 2007-08-07 11:43:05 +00:00
parent 491b47f0a9
commit 2bfeda7672
1 changed files with 26 additions and 12 deletions

View File

@ -166,29 +166,43 @@ function book_block($op = 'list', $delta = 0, $edit = array()) {
$block[0]['info'] = t('Book navigation'); $block[0]['info'] = t('Book navigation');
return $block; return $block;
case 'view': case 'view':
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
}
$current_bid = empty($node->book['bid']) ? 0 : $node->book['bid'];
$mode = variable_get('book_block_mode', 'all pages'); $mode = variable_get('book_block_mode', 'all pages');
if ($mode == 'all pages') { if ($mode == 'all pages') {
$block['subject'] = t('Book navigation'); $block['subject'] = t('Book navigation');
$book_menus = array(); $book_menus = array();
$pseudo_tree = array(0 => array('below' => FALSE));
foreach (book_get_books() as $book) { foreach (book_get_books() as $book) {
$book_menus[] = menu_tree_output(menu_tree_page_data($book['menu_name'])); if ($book['bid'] == $current_bid) {
// If the current page is a node associated with a book, the menu
// needs to be retrieved.
$book_menus[] = 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[] = menu_tree_output($pseudo_tree);
}
} }
$block['content'] = theme('book_all_books_block', $book_menus); $block['content'] = theme('book_all_books_block', $book_menus);
} }
elseif (arg(0) == 'node' && is_numeric(arg(1))) { elseif ($current_bid) {
// Only display this block when the user is browsing a book. // Only display this block when the user is browsing a book.
$node = node_load(arg(1));
if (isset($node->book['bid'])) {
$title = db_result(db_query(db_rewrite_sql('SELECT n.title FROM {node} n WHERE n.nid = %d'), $node->book['bid'])); $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. // Only show the block if the user has view access for the top-level node.
if ($title) { if ($title) {
$tree = menu_tree_all_data($node->book['menu_name'], $node->book); $tree = menu_tree_all_data($node->book['menu_name'], $node->book);
$data = array_shift($tree); // Should only be one element. // There should only be one element at the top level.
$data = array_shift($tree);
$block['subject'] = theme('book_title_link', $data['link']); $block['subject'] = theme('book_title_link', $data['link']);
$block['content'] = ($data['below']) ? menu_tree_output($data['below']) : ''; $block['content'] = ($data['below']) ? menu_tree_output($data['below']) : '';
} }
} }
}
return $block; return $block;
case 'configure': case 'configure':
$options = array( $options = array(