9

What hooks are available for altering the urls? For example, there are language modules that, when enabled, provide urls such as /en/node/1 rather than leaving it as /node/1.

I want to have something similar in the url, that I can have access to without having other functionality compromised.

How is this accomplished?

4
+50

So you're looking for a way to modify a link URL as it is being written? I looked into this recently and was surprised at how much hunting was required.

I went with hook_url_outbound_alter( &$path, &$options, $original_path ), which allows you to alter link URLs that are being written by the Drupal URL system.

There's also hook_url_inbound_alter( &$path, $original_path, $path_language ), which might be better as it includes the path's specific langauge - it depends on where you want the alteration to take place. I personally find hook_url_outbound_alter() reliable and simple to work with - it changes all appropriate URLs as they are written, and using $options you can reliably add and edit query strings (...?query=string&foo=bar) and hash frangments (...#someId).

Modify $path and it'll update the actual path that gets written into the URL href. Of course, this runs on all links, so you'll want to start by checking that it's a link you want to modify.

It's executed before aliasing, so it'll be based on the Drupal defaults. To give two very simplistic examples, if you want to modify only taxonomy paths:

function some_module_url_outbound_alter( &$path, &$options, $original_path ) {
  if(strpos($original_path, 'taxonomy/term') === 0) {
    // do stuff
  }
}

And/or if you only want it to run on particular pages, e.g. node edit pages:

function some_module_url_outbound_alter( &$path, &$options, $original_path ) {
  $current_path = explode("/", current_path() );
  if (count($current_path) >= 3 && $current_path[0] == "node" && $current_path[2] == "edit") {
    // do stuff
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.