Drupal 7 hook_menu用于特定内容类型

use*_*765 11 drupal drupal-7 drupal-routes

我尝试将新选项卡添加到特定内容类型'abc',这是代码,但它不起作用,选项卡显示在所有节点上.任何人都可以帮忙吗?谢谢!

function addtabexample_menu() {
  $items=array();

  $items['node/%node/test'] = array(
  'title' => 'Test',
  'page callback' => 'handle_test',
  'page arguments' => array('node', 1),
  'access arguments' => array('access content'), 
  'type' => MENU_LOCAL_TASK,
  'weight' => 100,
  );
return $items;
}

function handle_test($node){

  $result='hi';
  if ($node->type == 'abc') {
    $result='I am working';
}
Run Code Online (Sandbox Code Playgroud)

Cli*_*ive 12

access callback是决定是否显示选项卡的正确位置,但代码只是一个单行:

function addtabexample_menu() {
  $items = array();

  $items['node/%node/test'] = array(
    'title' => 'Test',
    'page callback' => 'handle_test',
    'page arguments' => array('node', 1),
    'access callback' => 'addtabexample_access_callback',
    'access arguments' => array(1), 
    'type' => MENU_LOCAL_TASK,
    'weight' => 100,
  );

  return $items;
}

function addtabexample_access_callback($node) {
  return $node->type == 'abc' && user_access('access content');
}
Run Code Online (Sandbox Code Playgroud)

一旦更改了代码,请记住清除缓存,hook_menu()以使更改生效.

  • 看看[hook_admin_paths()](http://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_admin_paths/7) (2认同)