Create a custom node tab with a custom path

2019-08-02 02:33发布

问题:

Is there anyway I can customize the paths to a node's tabs?

When using either pathauto or hook_menu_alter, I can change the path to the node view to from node/node_id to, well pretty much anything, but let's say xyz/node_title.

The node tabs, however, still stay with the path /node/node_id/tab_name

I'm trying to add a custom tab to the node, and keep the custom path as well (e.g.: xyz/node_title/tab_name instead of node/node_id/tab_name).

I manage to add the custom tab via hook_menu:

$items['node/%node/members'] = array(  
    'title' => 'Manage Membership',  
    'page callback' => 'mymodule_members',  
    'page arguments' => array(1),  
    'access callback'   => 'mymembers_members_access',  
    'access arguments' => array(1),  
    'type' => MENU_LOCAL_TASK  
); 

but if I try to customize the path, either at hook_menu or at hook_menu_alter, the tab just disappears.

Any ideas?

PS, I've posted the same question on the Drupal forums, no answer so far. I'll cross-update.

回答1:

You can add two functions in settings.php: custom_url_rewrite_inbound(), and custom_url_rewrite_outbound().

The examples in those pages should make clear how to use them.

function custom_url_rewrite_inbound(&$result, $path, $path_language) {
  global $user;

  // Change all article/x requests to node/x
  if (preg_match('|^article(/.*)|', $path, $matches)) {
    $result = 'node'. $matches[1];
  }
  // Redirect a path called 'e' to the user's profile edit page.
  if ($path == 'e') {
    $result = 'user/'. $user->uid .'/edit';
  }
}

function custom_url_rewrite_outbound(&$path, &$options, $original_path) {
  global $user;

  // Change all 'node' to 'article'.
  if (preg_match('|^node(/.*)|', $path, $matches)) {
    $path = 'article'. $matches[1];
  }
  // Create a path called 'e' which lands the user on her profile edit page.
  if ($path == 'user/'. $user->uid .'/edit') {
    $path = 'e';
  }
}

Drupal 7 uses two new hooks, instead of those functions: hook_url_inbound_alter() and hook_url_outbound_alter().



回答2:

I think this module will help you form the tab path the way you want: Sub-path URL Aliases.