3

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?

4

1 に答える 1

8

変更フックを使用したい場合hook_menu_alter():

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

既存のメニュー ルーター定義を変更するだけなので、変更する部分 (ページ コールバック関数名など) を宣言するだけです。Note も$items参照によって渡されるため、何も返す必要はありません。

于 2012-08-06T22:51:43.283 に答える