Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Drupal7中获得某个父级下的所有菜单项?_Php_Drupal_Module_Drupal 7_Drupal Modules - Fatal编程技术网

Php 如何在Drupal7中获得某个父级下的所有菜单项?

Php 如何在Drupal7中获得某个父级下的所有菜单项?,php,drupal,module,drupal-7,drupal-modules,Php,Drupal,Module,Drupal 7,Drupal Modules,我目前正在开发一个模块,该模块通过url路径查找父菜单项,然后通过查找顶级父菜单显示相关菜单的当前树结构,最重要的是仅显示该菜单中的菜单项和子菜单 一个简单的解决方案是通过所有项使用foreach循环,或者使用所有项作为键的数组 path = '/system/menu/submenu'; parent = 'system'; output = parent + parent submenus. “自定义菜单”中的所有菜单项: PHP代码应返回以下内容: - System - Menu

我目前正在开发一个模块,该模块通过url路径查找父菜单项,然后通过查找顶级父菜单显示相关菜单的当前树结构,最重要的是仅显示该菜单中的菜单项和子菜单

一个简单的解决方案是通过所有项使用foreach循环,或者使用所有项作为键的数组

path = '/system/menu/submenu';
parent = 'system';
output = parent + parent submenus.
“自定义菜单”中的所有菜单项:

PHP代码应返回以下内容:

- System
   - Menu wrapper
     - SubMenu 1A
     - SubMenu 2A
     - SubMenu 3A    
我的代码(当前不工作):

注意:这需要php代码和php代码只,所有其他模块不支持这只通过手动选择。内容通过块显示

“对于导航,Drupal将通过 菜单_导航_链接,只返回一级链接。 您可以按照此功能进入主菜单,然后 模板\u预处理\u在它之前的页面(这就是它作为一个 page.tpl.php中的变量)

但是,如果将菜单插入为块菜单_树_页面_数据菜单树调用(紧接着调用菜单树输出 这将对数组进行一些额外的工作,以获得最终标记。) 两个菜单都运行后,整个菜单树就可以作为 数组,然后我运行了一些定制的PHP代码来循环 数组并将其呈现到HTML列表中。“

以前有人问过这个问题,但我尝试过他们的解决方案,没有成功的可能原因是这些问题是针对Drupal6的。我目前正在与Drupal7合作

非常感谢你的帮助,谢谢

相关问题:



您首先需要的是您当前所在页面的mlid:

$q = variable_get('site_frontpage', 'node') == $_GET["q"] ? '<front>' : $_GET["q"];
$current_menu_item = db_select('menu_links' , 'ml')
  ->condition('ml.link_path' , $q)
  ->fields('ml', array('mlid', 'plid'))
  ->execute()
  ->fetchAll();
然后,您需要加载完整菜单:

$full_menu_items = menu_tree_all_data('main-menu');
现在循环浏览所有菜单项,只需获得我们想要的位:

foreach($full_menu_items as $menu_item) {
    if($menu_item['link']['mlid'] == $top_level_mlid) {
        $links = $menu_item['below'];
        break;
    }
}
$links现在保存您要查看的菜单特定部分的所有链接

要将该数组输出为页面上的菜单,请执行以下操作:

echo theme('links__system_secondary_menu', array(
    'links' => $links, 
    'attributes' => array(
        'id' => 'secondary-menu', 
        'class' => array('links', 'clearfix')), 
        'heading' => array(
            'text' => t('Secondary menu'), 
            'level' => 'h2', 
            'class' => array('element-invisible')
        )
));

像这样的怎么样

function your_module_menu_link__your_menu($variables){

    $element = $variables['element'];
    $sub_menu = '';

    if ($element['#below']) {

        $sub_menu = drupal_render($element['#below']);

    }

    $ouput = "";

    if($element['#original_link']['plid'] > 0 || $element['#below']){
        $link = l($element['#title'], $element['#href'], $element['#localized_options']);
        $ouput = '<li' . drupal_attributes($element['#attributes']) . '>' . $link . $sub_menu . "</li>\n";
    }

    return $ouput;

}
功能模块菜单链接菜单($variables){
$element=$variables['element'];
$sub_菜单=“”;
如果($element['#below'])){
$sub#u menu=drupal_render($element['#below']);
}
$ouput=“”;
如果($element['#原始链接']['plid']>0 | |$element['#以下'])){
$link=l($element['#title']、$element['#href']、$element['#本地化的#options']);
$ouput='.$link.$sub_菜单。“\n”;
}
返回$ouput;
}
foreach($full_menu_items as $menu_item) {
    if($menu_item['link']['mlid'] == $top_level_mlid) {
        $links = $menu_item['below'];
        break;
    }
}
echo theme('links__system_secondary_menu', array(
    'links' => $links, 
    'attributes' => array(
        'id' => 'secondary-menu', 
        'class' => array('links', 'clearfix')), 
        'heading' => array(
            'text' => t('Secondary menu'), 
            'level' => 'h2', 
            'class' => array('element-invisible')
        )
));
function your_module_menu_link__your_menu($variables){

    $element = $variables['element'];
    $sub_menu = '';

    if ($element['#below']) {

        $sub_menu = drupal_render($element['#below']);

    }

    $ouput = "";

    if($element['#original_link']['plid'] > 0 || $element['#below']){
        $link = l($element['#title'], $element['#href'], $element['#localized_options']);
        $ouput = '<li' . drupal_attributes($element['#attributes']) . '>' . $link . $sub_menu . "</li>\n";
    }

    return $ouput;

}