Drupal Answers is a question and answer site for Drupal developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In this answer I wanted to alter a URL using hook_file_url_alter(), and within my implementation of that hook I needed to convert the URI to a URL.

Normally this would be done with a call to file_create_url() but that is not possible here as it would trigger my hook causing an endless loop.

I can see at the top of drupal_alter() it is using the static variable $drupal_static_fast which makes me think maybe there is a way of using this to temporarily disable a hook being called, but I'm not sure.

share|improve this question
up vote 3 down vote accepted

You can't stop the hooks from being invoked unless you edit drupal_alter (or file_create_url) directly.

But you can use static vars to short circuit your own implementation so it only runs once for a given $uri:

function MYMODULE_file_url_alter(&$uri) {
  $processed_uris = &drupal_static(__FUNCTION__, array());

  if (!in_array($uri, $processed_uris)) {
    $processed_uris[] = $uri;

    // Even though this call will re-invoke the current function, the URI has been 
    // processed, so no infinite loop.
    $foo = file_create_url($uri);
share|improve this answer
Clever, very clever. I'll have to remember this :) – Felix Eve Apr 28 '14 at 10:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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