how to override Drupal path from another module

2019-05-10 06:12发布

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条回答
劫难
2楼-- · 2019-05-10 06:24

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.

查看更多
登录 后发表回答