如何从另一个模块覆盖Drupal路径 [英] how to override Drupal path from another module
问题描述
我想要我的模块覆盖由另一个模块设置的路径
示例:
模块A已经注册了一个路径:
$ menu ['node /%id / test'] = array(
pre>
'title '=>'Test',
'page callback'=>'test_A',
'page arguments'=>数组(1),
'access callback'=> 'test_access',
'access arguments'=> array(1),
'type'=> MENU_LOCAL_TASK,
)
现在我创建模块B并注册相同的路径。
$ menu ['node /%id / test'] = array(
'title'=>'Test',
'page callback'=>'test_B',
'页面参数'=>数组(1),
'访问回调'=>'test_access',
'访问参数'=>数组(1),
'type'= > MENU_LOCAL_TASK,
)
对此路径的每个请求
www.mysite.com/node/1/test
将路由模块B不是A。
其他模块设置了覆盖现有路径的最佳方法是什么?
解决方案你想使用一个alter hook,
hook_menu_alter()
:function mymodule_menu_alter(& $ items){
$ items ['node /%id / test'] ['page callback'] ='test_B';
}
由于您只是修改现有的菜单路由器定义,您只需要声明要更改的部分(例如,页面回调函数名称)。注意
$ items
通过引用传递,所以你不需要返回任何东西。I want my module to override the path was set by another module
Example:
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, )
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, )
Every request to this path
www.mysite.com/node/1/test
will route to module B not A.
What is the best way to override an existing path was set by other module?
解决方案You want to use an alter hook,
hook_menu_alter()
:function mymodule_menu_alter(&$items) { $items['node/%id/test']['page callback'] = 'test_B'; }
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屋!