1

I run into strange thing today. I have some custom hook declared in a custom module as following:

drupal_alter('strange_thing', $shared_var, $vars);

then in template file(template.php) I got the function:

function THEME_strange_thing_alter(&$shared_var, $vars){
    //do something with $shared_var
}

The THEME_strange_thing_alter() is called if i have unchecked " Use the administration theme when editing or creating content" in the admin/appearance in the "Administration Theme" section, but it is not otherwise.

To satisfy curiosity, I moved all the logic from tempalate.php into custom module/.module file and it works like expected...the hooks are called not matter if the box is checked or unchecked for " Use the administration theme when editing or creating content"

It would be great that the hooks work in template.php no matter if there is some box checked or unchecked in the UI. Is there way to make it so? Thank You

1 Answer 1

4

Hooks are only invoked for enabled modules, and the current theme. So code in template.php for theme1 won't be used when theme2 is active (unless theme2 is a subtheme of theme1, thanks @greggles).

The reason is obvious when you think about it: if you've explicitly said you want one theme on a certain page, code from another theme shouldn't be allowed to interfere. It could cause all sorts of problems.

If you have code that should run independently of the theme that's being used to display the page, the proper (and only) place to put it is in a module.

2
  • 1
    Is this rule still true if theme2 is a subtheme of theme1?
    – greggles
    Sep 29, 2014 at 22:28
  • Good point @greggles, no it isn't. I've updated the answer, thanks
    – Clive
    Sep 29, 2014 at 22:32

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.