Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.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
drupal hook_菜单_alter()用于添加选项卡_Drupal_Menu_Tabs_Hook_Alter - Fatal编程技术网

drupal hook_菜单_alter()用于添加选项卡

drupal hook_菜单_alter()用于添加选项卡,drupal,menu,tabs,hook,alter,Drupal,Menu,Tabs,Hook,Alter,我想在名为“cssswitch”的模块中的“node/%/edit”页面中添加一些选项卡。 当我单击“重建菜单”时,将显示两个新选项卡,但在编辑它们时,它们将显示给所有节点,而不仅仅是节点“cssswitch”。我希望这些新选项卡仅在编辑“cssswitch”类型的节点时显示 另一个问题是,当我清除所有缓存时,选项卡从所有编辑页面完全消失。下面是我写的代码 function cssswitch_menu_alter(&$items) { $node = menu

我想在名为“cssswitch”的模块中的“node/%/edit”页面中添加一些选项卡。 当我单击“重建菜单”时,将显示两个新选项卡,但在编辑它们时,它们将显示给所有节点,而不仅仅是节点“cssswitch”。我希望这些新选项卡仅在编辑“cssswitch”类型的节点时显示

另一个问题是,当我清除所有缓存时,选项卡从所有编辑页面完全消失。下面是我写的代码

    function cssswitch_menu_alter(&$items) {

        $node = menu_get_object();
        //print_r($node);
        //echo $node->type; //exit();
        if ($node->type == 'cssswitch') {

            $items['node/%/edit/schedulenew'] = array(
                'title' => 'Schedule1',
                'access callback'=>'user_access',
                'access arguments'=>array('view cssswitch'),
                'page callback' => 'cssswitch_schedule',
                'page arguments' => array(1),
                'type' => MENU_LOCAL_TASK,
                'weight'=>4,
            );

            $items['node/%/edit/schedulenew2'] = array(
                'title' => 'Schedule2',
                'access callback'=>'user_access',
                'access arguments'=>array('view cssswitch'),
                'page callback' => 'cssswitch_test2',
                'page arguments' => array(1),
                'type' => MENU_LOCAL_TASK,
                'weight'=>3,
            );  


        }

    }

function cssswitch_test(){
    return 'test';
}

function cssswitch_test2(){
    return 'test2';
}
感谢您的帮助。

hook\u menu\u alter()仅在菜单生成过程中调用,因此您无法在该函数中执行动态节点类型检查

但是,要实现您想要的功能,您可以通过自定义访问回调来实现,如下所示:

       // Note, I replaced the '%' in your original code with '%node'. See hook_menu() for details on this.
       $items['node/%node/edit/schedulenew2'] = array(
            ...
            'access callback'=>'cssswitch_schedulenew_access',
            // This passes in the $node object as the argument.
            'access arguments'=>array(1),
            ...
        );  
然后,在新的自定义访问回调中:

function cssswitch_schedulenew_access($node) {
  // Check that node is the proper type, and that the user has the proper permission.
  return $node->type == 'cssswitch' && user_access('view cssswitch');
}

对于其他节点类型,此函数将返回false,从而拒绝访问,从而删除选项卡。

+1-仅需另外注意:
hook\u menu\u alter()
用于更改其他模块提供的菜单项。由于OP想要添加新条目,他应该使用
hook\u menu()
。谢谢,这正是我想要的。我现在明白它是怎么工作的了。