3

I have the following code in the theme template.php file:

function MYTHEME_item_list(&$variables) {
  // my custom stuff done to theme_item_list 
  return $output;
}

And also this:

function MYTHEME_theme($existing, $type, $theme, $path) {
  return array(
    'item_list' => array(
      'variables' => array(),
    ),
  );
}

I have cleared cache many times.

Looking at the theme registry I can see that my override is getting picked up:

[item_list] => Array
  (
    [function] => MYTHEME_item_list
    [type] => theme_engine
    [theme path] => profiles/eurostar/themes/eurostar
    [variables] => Array
      (
        [items] => Array
          (
          ) 
        [title] => 
        [type] => ul
        [attributes] => Array
          (
          ) 
      )
    [preprocess functions] => Array
      (
      )
    [process functions] => Array
      (
      )
  )

Unfortunately none of this works. Does anyone have an idea of what I'm doing wrong?

UPDATE:
So, this works: theme('item_list', $variables);

This does not: theme_item_list($variables);

1
  • 1
    If you call the function directly (theme_item_list()) then it doesn't go through the theme system so your override will never work...that's why it's always recommend to use the theme() function to wrap around the theme call.
    – Clive
    Apr 19, 2012 at 11:46

1 Answer 1

3

You don't need to implement hook_theme() if you just want to override an existing theme function.

I've just tried this code in my template.php file:

function MYTHEME_item_list(&$variables) {
  // my custom stuff done to theme_item_list 
  return 'test';
}

And all calls to theme('item_list') now return the string 'test', and nothing else.

You'll need to clear Drupal's caches once you've implemented the function, that might also be an issue.

5
  • Thanks for getting back to me. I've removed the hook_theme but it still isn't working. Any other ideas? Apr 19, 2012 at 10:52
  • 'Fraid not, as I said I've tested the above on a blank Drupal installation and it works perfectly. All I can advise is clear Drupal's caches so the theme registry gets rebuilt :/
    – Clive
    Apr 19, 2012 at 10:59
  • Have you tried just returning the string 'test' from the function? It would rule out any errors in your logic to build up the $output variable.
    – Clive
    Apr 19, 2012 at 11:06
  • Yup. I was actually going for a print_r of the $variables array and then die :) Apr 19, 2012 at 11:16
  • It must be another module/subtheme causing the problem then. Try disabling the contrib modules you've got installed one by one to see which one might be causing the problem. Your code is completely correct and is in the right file (and works for me without any extra effort), so the problem must be with another module/theme interfering
    – Clive
    Apr 19, 2012 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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