I have created a theme that contains several functions.

I now want to create a sub theme. That way, I can use the parent theme on all my sites (and any updates to the parent the theme can be easily applied to all the sites). The sub theme will contain all the modifications that are specific to that individual website.

I have created a sub theme following the instructions in this tutorial. When I enable the subtheme, the site breaks. Most of the content disappears and I get this error message:

Fatal error: Call to undefined function legendary_views_field_compare_value() in /all/themes/legendary/templates/slideshow.tpl.php on line 20

  • legendary_views_field_compare_value() is the name of a function in my parent theme.
  • legendary is the name of my parent theme.
  • slideshow.tpl.php is a template in my parent theme.

I don't understand why this is happening. I thought the sub theme should inherit all the functions of the parent theme (especially is the function is being called from inside the parent theme and NOT in the sub theme).

How can I fix this?

  • Where did you define the function? I suppose that you did it in the template.php – Rufein Dec 12 '13 at 16:32
  • @Rufein Yes, the function is defined in template.php in the parent theme and is called from a template file inside of the parent theme. – big_smile Dec 12 '13 at 16:41
  • I have a suggestion. The instructions that you have been following warn that There are two main types of functions in template.php: theme function overrides and preprocess functions . Taking a look to the theme.inc, i've found this function ´drupal_find_theme_functions()´, that uses the cache to store the hooks defined in your themes and modules. So, i am wondering that Drupal only load the template.php when the the result is true in that function. A solution: you separate the custom function in a file, for example ´myfiles.inc´ and add in the .info theme that file – Rufein Dec 12 '13 at 17:49
  • @Rufein would myfiles.inc go in the theme directory. Also how do you a reference an Inc file in a .info file. I've looked at this tutorial about .info files, but it doesn't say how to add inc files in an info file. Thanks for any help you can offer. – big_smile Dec 12 '13 at 18:02
  • You are right, the theme .info doesn't support files[] directive, i was thinking in module .info. But don't worry there are more ways to load files. For example, i would use a preproccess function to load the file with include_once – Rufein Dec 12 '13 at 18:25

The problem

Pay attention to your first sentence:

I have created a theme that contains several functions.

The link that you have provided warns about what type of functions hold a template.php in a theme.

There are two main types of functions in template.php: theme function overrides and preprocess functions. The template system handles these two types in very different ways.

Is legendary_views_field_compare_value() any of these cases? OK, we agree that the anwers is No. It's a custom function that you have written.

Why should not you put your custom functions in template.php?

Because Drupal has a registry of implemented hooks and templates that have been overriden. The system only load the template.php when match the hook in the registry. So, the template.php is not always load.

The solution

First of all, you should keep separate the custom functions from the theme functions. You need to create a file called for example my_custom_functions.inc , and insert all your custom functions in that file.

How do i load a file with custom functions that i need to call in my templates?

To keep separate the logic from the theme functions, i would stored the custom functions in a module. But, it is also possible store the functions in your theme. In both cases, you have to follow the next strategy.

It's really easy. Every time that a template is rendered, a preprocess function is fired by default to let other modules to act. So, it is always fired a preprocess (and process) function before the slideshow.tpl,php. In this moment, we will load the file with our custom functions.

Here an example:

 *  Implements hook_preprocess_slideshow()
 *  The file will load  before rendering slideshow.tpl.php
 function NAME_OF_YOUR_THEME_preprocess_slideshow(&$variables){

     include_once 'my_custom_functions.inc';

Please, pay attention to the name of the template. In my simulation, i have changed the template called views-slideshow.tpl.php . So, when you implements in hook_preprocess_HOOK(&$vars) , you need to change the hyphens. The result would be NAME_OF_YOUR_THEME_preprocess_views_slideshow(&$vars)

If you have to load the same file with custom functions in lots of templates, you could implement the general hook_preprocess($vars), that it is always fired. Here an example:

 *  Implements hook_preprocess()
 *  The system always load the file
 function NAME_OF_YOUR_THEME_preprocess(&$variables){

     include_once 'my_function.inc';
  • Where does My-fucntion.inc go? Does it go in the theme directory? Thanks for any help you can offer. – big_smile Dec 12 '13 at 18:42
  • 1
    Yes, it should be in the same directory of template.php – Rufein Dec 12 '13 at 18:46
  • 1
    I forgot: you have to clean the cache. Don´t add it in the preprocces of page or html, because hey are the last templates to be rendered in the whole proccess, so when these preproccess functions have been called,the slideshow has been rendered before – Rufein Dec 12 '13 at 19:50
  • 1
    Is it working? If it's working and you want to be available in a lot of templates, the fastest way is implement hook_preprocess(vars), that is a generic hook that is always called before render a template. – Rufein Dec 12 '13 at 20:30
  • 1
    I've done a simulation. First i have created a Parent theme based on Bartik and a child Theme. I have set the child theme as default theme. The functions on my template in the parent template.php haven been called and the include file is loaded. So, I guarantee you that this way to load the file is fine. Maybe there are a misspelled name. – Rufein Dec 13 '13 at 14:32

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.