Subtheme: How to use the template function of a parent theme (from inside the parent theme) 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?
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(&amp;$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(&amp;$vars) , you need to change the hyphens. The result would be NAME_OF_YOUR_THEME_preprocess_views_slideshow(&amp;$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(&amp;$variables){

     include_once 'my_function.inc';
 }