Handle contexts in custom_url_???

Events happening in the community are now at Drupal community events on www.drupal.org.
fractile81's picture

Great presentation on contexts. I've dabbled with the custom_url_rewrite(); function (and the equivalents in Drupal 6), and it can be pretty intense if the wrong logic is used (you don't want to be doing a DB query for outbound URLs, for example). This function basically gets called once to modify the URL called by the browser, then X number of times (based on number of l(); function called for a page).

So I was wondering if context_get(); and context_set(); were being used in both use cases of the custom_url_rewrite(); function? So, the 'source' case does the context_set(); and the 'alias' case pulls back the current context and properly forms the outbound URL (possibly cache this locally in the function?).

The only logic really needed there would be to make sure certain context-free links don't inherit the current page's context (e.g. the main site's homepage).

Was this how the presenters had this setup? Either way, thoughts about this approach?

-Fractile81

Comments

Using custom_url_rewrite()

yhahn's picture

Yes, you won't want to do any heavy logic (and definitely no queries) in custom_url_rewrite. Hopefully you already know the pieces you want to rewrite with (your language prefix for example) and just need to tack it on intelligently.

Probably the best place to look for how to use custom_url_rewrite well is in the i18n from Drupal 5 (this code has been moved into core in Drupal 6). Here is a snippet from i18n:

if(!function_exists('custom_url_rewrite')) {
  function custom_url_rewrite($type, $path, $original) {
    return i18n_url_rewrite($type, $path, $original);
  }


function i18n_url_rewrite($type, $path, $original){
  if ($type == 'alias' && !i18n_get_lang_prefix($path) ){
    return $path ? i18n_get_lang() . '/'. $path : i18n_get_lang();
  } elseif ($type == 'source') {
    if ($path == $original) {
      return i18n_get_normal_path($path);
    } else { // Path may have been dealiased but still have language prefix
      i18n_get_lang_prefix($path, TRUE);
      return $path;
    }
  } else {
    return $path;
  }
}

It is worth noting that in Drupal 5 custom_url_rewrite() does ask you to think through if you want to handle aliased urls differently from source urls.

The other piece you'll want to think about is stripping out your prefix on hook_init(). You can take a look at i18n_init() as an example, but the basic idea is to parse the url path on page load, grab the first arg and set the language context (if it is a language prefix). i18n then strips out the prefix, and passes the rest of the path onto Drupal:

    //search alias with and without lang and remove lang.
    $_GET['q'] = i18n_get_normal_path($path);

We're working on a releasable version of a basic context module soon, and I am concepting out a more generalized context_prefix module so hopefully we can make this easier for you.