drupal hook_menu_alter() 用于添加选项卡

发布于 2024-09-02 20:59:41 字数 1393 浏览 5 评论 0原文

我想在名为“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';
}

感谢您的任何帮助。

I want to add some tabs in the "node/%/edit" page from my module called "cssswitch".
When I click "Rebuild Menus", the two new tabs are displayed, but they are displayed for ALL nodes when editing them, not just for the node "cssswitch". I want these new tabs to be displayed only when editing node of type "cssswitch".

The other problem is when I clear all cache, the tabs completely dissapear from all edit pages. Below is the code I wrote.

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

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贵在坚持 2024-09-09 20:59:41

hook_menu_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,从而拒绝访问,从而删除选项卡。

hook_menu_alter() is only called during the menu building process, so you can't do dynamic node type checks within that function.

However, to achieve what you want, you can do this with a custom access callback as follows:

       // 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),
            ...
        );  

Then, in your new custom access callback:

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');
}

For other node types, this function will return false, thus denying access, and thus removing the tab.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文