how to override Drupal path from another module

独自空忆成欢 提交于 2019-12-07 14:46:04

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/11837048/how-to-override-drupal-path-from-another-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!