【问题标题】:How can Drupal nice menus be used to display a book menu at any level?如何使用 Drupal nice menus 来显示任何级别的书籍菜单?
【发布时间】:2013-04-19 08:58:40
【问题描述】:

我想使用 Drupal Nice Menus 显示某些书籍或任何级别的子书籍,但看不到 theme_nice_menus 函数将如何做到这一点。

我正在查看下面的代码示例以获得一些灵感:

第一个示例适用于所有书籍,但我需要传递选定的书籍,甚至是子书籍。

第二个例子适用于一本书,但没有使用漂亮的菜单? menu_tree_all_datamenu_tree_output 的输出可以传递给theme_nice_menus 函数吗?

第三个来自 github 上的 bookoblock 模块,它显示了如何从它的孩子那里获取一本单独的书,而不是从它的孩子那里获取子书,并且不使用漂亮的菜单。

我正在尝试创建这些示例中的一些内容。从子书而不是顶级书生成菜单是我想做的一件事。

<?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;
}

?>

【问题讨论】:

    标签: drupal menu


    【解决方案1】:

    我决定使用第一种方法,修改 Drupal API book_get_books 函数的代码以仅选择所需的书籍。

    <?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;
    ?>
    

    【讨论】:

    • 我尝试将书籍 ID 列表作为参数添加到 db_query,但无法正常工作。什么是正确的语法?
    猜你喜欢
    • 1970-01-01
    • 2021-04-30
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多