0
votes

I want to use Drupal Nice Menus to display certain books, or sub books at any level, but can't see how the theme_nice_menus function will do that.

I am looking at the code samples below for some inspiration:

The first example works an all the books, but I need to pass on selected books, or even a sub book.

The second example works on a single book, but doesn't use nice menu? Can the output of menu_tree_all_data or menu_tree_output be passed to the theme_nice_menus function?

The third one which is from bookoblock module on github displays how to get an individual book, but not a sub book from its children, and doesn't use nice menus.

I am trying to create something of these examples. Generating a menu from a sub book rather than a top level book is one thing I would want to do.

<?php
$master_menu = '';
$books = book_get_books();
foreach($books as $book_nid=>$book) {
  $menu = theme('nice_menus', array('id' => $book['mlid'], 'direction' => 'right', 'depth' => -1, 'menu_name' => $book['menu_name'], 'menu' => NULL));
  $master_menu .= $menu['content'];
}
print $master_menu;
?>

<?php
  $book_top_page= 49;
  $tree = menu_tree_all_data(book_menu_name($book_top_page));
  print drupal_render(menu_tree_output($tree));
?>

<?php
function bookoblock_block_view() {
  if ($book = bookoblock_is_book_node()) {
    // menu_build_tree() doesn't accept zero for depth, so we convert that to
    // NULL and add 1 if it's not 0 to account for the first (skipped) level.
    $max_depth = variable_get('bookoblock_depth', NULL);
    $max_depth = ($max_depth == 0) ? NULL : ($max_depth + 1);

    // Vars and params for the menu_build_tree() function.
    $path = 'node/' . $book['bid'];
    $parent = menu_link_get_preferred($path, book_menu_name($book['bid']));
    $parameters = array(
      'only_active_trail' => FALSE,
      'min_depth' => $parent['depth'] + 1,
      'max_depth' => $max_depth,
    );

    // Build the tree and block title.
    $children = menu_build_tree($parent['menu_name'], $parameters);
    $book_name = (book_toc($book['bid'], 1));

    // Build and return the $block array.
    $block['subject'] = l($book_name[$book['p1']], 'node/' . $book['bid']);
    $block['content'] = menu_tree_output($children);
    return $block;
  }
  // If the current node isn't part of a book, just return nothing.
  return NULL;
}

?>
1

1 Answers

0
votes

I settled on using the first approach, adapting the code of the Drupal API book_get_books function to select only the books required.

<?php
$master_menu = '';
unset($four_books);
  $four_books_list = "91,323,47,149";
  $four_books = &drupal_static(__FUNCTION__);
  if (!isset($four_books)) {
    $four_books = array();
    $nids = db_query("SELECT DISTINCT(bid) FROM {book} where bid in (91,323,47,149)")->fetchCol();

    if ($nids) {
      $query = db_select('book', 'b', array('fetch' => PDO::FETCH_ASSOC));
      $query->join('node', 'n', 'b.nid = n.nid');
      $query->join('menu_links', 'ml', 'b.mlid = ml.mlid');
      $query->addField('n', 'type', 'type');
      $query->addField('n', 'title', 'title');
      $query->fields('b');
      $query->fields('ml');
      $query->condition('n.nid', $nids, 'IN');
      $query->condition('n.status', 1);
      $query->orderBy('ml.weight');
      $query->orderBy('ml.link_title');
      $query->addTag('node_access');
      $result2 = $query->execute();
      foreach ($result2 as $link) {
        $link['href'] = $link['link_path'];
        $link['options'] = unserialize($link['options']);
        $four_books[$link['bid']] = $link;
      }
    }
  }

foreach($four_books as $book_nid=>$book) {
  $menu = theme('nice_menus', array('id' => $book['mlid'], 'direction' => 'right', 'depth' => -1, 'menu_name' => $book['menu_name'], 'menu' => NULL));
  $master_menu .= $menu['content'];
}
print $master_menu;
?>