0

I have some theme functions defined in my theme's template.php file that I am calling in my modules with no problems.

I also have some theme functions defined in my_module.module that are also working.

I'm doing some work on themed automated emails with custom rules actions and I found that when rules calls theme functions defined in my theme's template.php file they don't return anything, but when calling the same functions copied and pasted to my_module.module the return what I expect.

I also found that bypassing rules and doing the same process (calling the functions from both locations) in a hook_nodeapi in the 'presave' case gives me the same behaviour. (Strangely however they work in the 'load' case.)

Is there a way I can have my theme functions neatly defined in my theme's template.php where they are already working for all my other modules, and still get at them with rules / hook_nodeapi?

Thanks!

EDIT - more info:

Thanks guys. The reason I put them in the theme to begin with is because I'm working on a multi-domain profile with a parent theme and a subtheme for each sub domain. They were template functions for html emails that need to be branded in with images and styles from each subtheme. I've had problems with putting template theme functions in modules because the path to the templates defaults to the module directory, and I need the template to be selected dynamically based on which subtheme it was called from. Thats why I wanted to define them in the parent theme so they could be overridden per child theme.

  • 1
    A module shouldn't rely on a theme, if anything it would be the other way around. You should move your functions into an enabled module file, and then they'll be available to both the theme and other modules – Clive Jul 23 '12 at 9:52
1

Functions defined inside of template.php will not reliably be available (nor are they meant to be) outside of the theme. When it comes to overriding preexisting theme functions, the overrides created within template.php will automatically be applied by Drupal core, there is no reason to actually invoke the function.

For example, if you wanted to override the functionality of theme_menu(), and you created in your theme's template.php a function named mytheme_menu(), that new function will not actually be invoked anywhere within the site's code. The function that is invoked will still be theme_menu() and Drupal core will automatically override that invocation with the functionality within mytheme_menu(). This is the intended use of the template.php file; to override existing themable output, not to create new theme functions.

Now, if you create a custom element and need to create a brand new theme function for that custom element, this function must be declared within the context of a custom module. For example, you would need to define the function theme_myelement() inside of mymodule.module. You will also need to implement hook_theme() by defining the function mymodule_theme() in order to tell the Drupal theme system about your new function. There's a bit more to it, which is explained in the Drupal module developer's handbook for Drupal 6 and Drupal 7.

If this doesn't answer your question, please edit your question to provide specific details about your custom module and custom theme functions.

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.