开发者

Using module: url_alter and it's hook: hook_url_outbound_alter()

hook_url_outbound_alter()

I am struggling to find any documentation or explanation for how to use this hook. I have a page: node/1221 and when it look like so: node/1221/profile/chn/3 It loads a profile for that user.

I want pretty URL's so that when the user visits departments/pch/chn/profile/3 I want it to actually load node/1221/profile/chn/3

I am pretty sure the hook should help me with this but not sure how it works.

Note: Using drupal 6, tried aliases but they didn't work.

2nd update:

The 3 in the URL is a profile ID that I want to pass on to another URL. I have node/1221/profile/chn/3 working because it has a panel that grabs the %4 argument and passes it to a view inside the panel. Hope this gives more context.

Currently trying:

/**
 * Implementation of hook_boot()
 */
function pathargs_boot() {
  // remain empty, only needed to let Drupal bootstrap system know to load this module earlier.
}

/**
 * Will define custom_url_rewrite_inbound(). If url_alter is enabled 
 * Pathargs inbound alter will be called by its implementation of 
 * custom_url_rewrite_inbound() instead.
 */
if (!function_exists('custom_url_rewrite_inbound')) {
  function custom_url_rewrite_inbound(&$result, $path, $path_language) {
    return pathargs_url_inbound_alter($result, $path, $path_language);
  }
}

/**
 * Implementation of hook_url_inbound_alter()
 */
function pathargs_url_inbound_alter(&$result, $path, $path_la开发者_如何学编程nguage) {
  watchdog('Path Arguments', "$path + $original_path");
  if($result == 'chn') {
    $result = 'node/1222/chn/profile/3';
  }
}

Still not working... visiting www.domain.com/chn does nothing.


Try this: http://drupal.org/project/subpath_alias According to that module's description it does what you want it to do.


If you want to use hook_url_outbound_alter() you need to upgrade to Drupal 7 since it doesn't exist on D6 and earlier.

If I were you, I would use aliases. Aliases work by rewriting the url to that departments/pch/chn/profile/3 would be translated into node/1221/profile/chn/3 which would be a valid Drupal path for your installation.

The thing is that what you got is a bit site specific, so you'll need to create your own system to create the aliases in your custom module instead of relying on path auto.

I don't know the semantics of your urls, so I can't help you how you should create the aliases. Most likely you can do it when a new node is created. Take a look at pathauto_create_alias it should help you along most of the way.

Using aliases should be more maintainable and some modules hook into this allowing you to do other things. Using custom_url_rewrite_xxx can quickly become messy quick easily, especially if you need to add a lot of rules. It will probably be quicker to use now, but if you have to maintain the site, this is something that can lead some serious WTF 1 year from now.

hook_url_outbound_alter

If you do decide to take this route, you need to change the $path variable and not the $result. You can take a look at the url_alter.api.php for guidance, an example of usage is:

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜