【问题标题】:Create a custom node tab with a custom path使用自定义路径创建自定义节点选项卡
【发布时间】:2010-06-19 13:05:57
【问题描述】:

我是否可以自定义节点选项卡的路径?

当使用 pathauto 或 hook_menu_alter 时,我可以将节点视图的路径从 node/node_id 更改为,几乎任何东西,但假设是 xyz/node_title。

但是,节点选项卡仍然保留在路径 /node/node_id/tab_name

我正在尝试向节点添加自定义选项卡,并保留自定义路径(例如:xyz/node_title/tab_name 而不是 node/node_id/tab_name)。

我设法通过 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  
); 

但如果我尝试在 hook_menu 或 hook_menu_alter 自定义路径,选项卡就会消失。

有什么想法吗?

PS,我已经在 Drupal 论坛上发布了the same question,目前还没有答案。我会交叉更新。

【问题讨论】:

    标签: drupal drupal-6 menu


    【解决方案1】:

    您可以在settings.php中添加两个函数:custom_url_rewrite_inbound()custom_url_rewrite_outbound()

    这些页面中的示例应该清楚地说明如何使用它们。

    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 使用了两个新的钩子,而不是那些函数:hook_url_inbound_alter()hook_url_outbound_alter()

    【讨论】:

    • 我最终使用了这个答案以及 url_alter 模块(它允许我保持 settings.php 的清洁)。 subpath_alias 模块与其他自定义代码合并。
    • @kiamlaluno,Drupal 7 使用什么?
    • @kiamlaluno,如果你想获得一些额外的声誉,你可以回答我的问题stackoverflow.com/questions/8743071/use-node-menu-on-page :)
    【解决方案2】:

    我认为这个模块将帮助你按照你想要的方式形成标签路径:Sub-path URL Aliases

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多