| 7 system.api.php | hook_url_outbound_alter(&$path, &$options, $original_path) |
| 8 system.api.php | hook_url_outbound_alter(&$path, &$options, $original_path) |
Alters outbound URLs.
Parameters
$path: The outbound path to alter, not adjusted for path aliases yet. It won't be adjusted for path aliases until all modules are finished altering it, thus being consistent with hook_url_inbound_alter(), which adjusts for all path aliases before allowing modules to alter it. This may have been altered by other modules before this one.
$options: A set of URL options for the URL so elements such as a fragment or a query string can be added to the URL.
$original_path: The original path, before being altered by any modules.
See also
url()
Related topics
2 functions implement hook_url_outbound_alter()
1 invocation of hook_url_outbound_alter()
File
- modules/
system/ system.api.php, line 4141 - Hooks provided by Drupal core and the System module.
Code
function hook_url_outbound_alter(&$path, &$options, $original_path) {
// Use an external RSS feed rather than the Drupal one.
if ($path == 'rss.xml') {
$path = 'http://example.com/rss.xml';
$options['external'] = TRUE;
}
// Instead of pointing to user/[uid]/edit, point to user/me/edit.
if (preg_match('|^user/([0-9]*)/edit(/.*)?|', $path, $matches)) {
global $user;
if ($user->uid == $matches[1]) {
$path = 'user/me/edit' . $matches[2];
}
}
}
Login or register to post comments
Comments
This replaces
This replaces custom_url_rewrite_outbound() from Drupal 6.
If you want to override an existing url_alias:
If you want to override an existing url_alias:
- change your $path as you need
- set $options['alias'] = $path;
This prevents url() in common.inc from running a drupal_lookup_path(), effectively changing the url_alias (temporarily, the url_alias is not changed).
A favorite example of mine
This example makes it so that login and logout links everywhere on the site (that run through url() of course) always take the user back to the page that they clicked login or logout.
<?php/**
* Implements hook_url_outbound_alter().
*/
function mymodule_url_outbound_alter(&$path, &$options, $original_path) {
// Always make login/logout links go to the current page.
switch ($path) {
case 'user/logout':
case 'user/login':
$options['query']['destination'] = $_GET['q'];
break;
}
}
?>