如何从另一个模块覆盖 Drupal 路径 [英] how to override Drupal path from another module

查看:17
本文介绍了如何从另一个模块覆盖 Drupal 路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的模块覆盖由另一个模块设置的路径

I want my module to override the path was set by another module

例子:

模块 A 已经注册了一个路径:

Module A has register a path:

$menu['node/%id/test'] = array(
    'title' => 'Test',
    'page callback' => 'test_A',
    'page arguments' => array(1),
    'access callback' => 'test_access',
    'access arguments' => array(1),
    'type' => MENU_LOCAL_TASK,
)

现在我创建模块 B 并注册相同的路径.

Now I create module B and register the same path.

$menu['node/%id/test'] = array(
    'title' => 'Test',
    'page callback' => 'test_B',
    'page arguments' => array(1),
    'access callback' => 'test_access',
    'access arguments' => array(1),
    'type' => MENU_LOCAL_TASK,
)

对该路径的每个请求

www.mysite.com/node/1/test

www.mysite.com/node/1/test

将路由到模块 B 而不是 A.

will route to module B not A.

覆盖由其他模块设置的现有路径的最佳方法是什么?

What is the best way to override an existing path was set by other module?

推荐答案

你想使用一个改变钩子,hook_menu_alter():

You want to use an alter hook, hook_menu_alter():

function mymodule_menu_alter(&$items) {
  $items['node/%id/test']['page callback'] = 'test_B';
}

由于您只是修改现有的菜单路由器定义,因此您只需声明要更改的部分(例如,页面回调函数名称).还要注意 $items 是通过引用传递的,所以你不需要返回任何东西.

Since you're just modifying an existing menu router definition, you only need to declare the part you want to change (e.g., the page callback function name). Note also $items is passed by reference, so you don't need to return anything.

这篇关于如何从另一个模块覆盖 Drupal 路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆