I have a menu that calls a function.
This function returns some markup.
The markup is made up from some hardcode hrml and the results of a call to a theme function.
All goes well for a while and then intermittently the preprocessor function that should get called in theme does not
get called, resulting in missing html on my page.
There is no drupal caching set for this site.
So: users goes to www.mysite/partners/info_man/home/tio
this calls partners_im_tio_menu_page(),
which calls theme('partners_tio_client_list') which results
in partners_preprocess_partners_tio_client_list in partners_tio_client_list.inc
and uses partners-tio-client-list.tpl.php being called.

This goes fine but periodically I need to clear table and theme cache - some times multiple times to get the theme call
to return any output.
Can anyone explain what I am doing wrong?

// partners module:

// include the preprocessor function
include_once drupal_get_path('module', 'partners'). '/partners_tio_client_list.inc';

// theme hook
function partners_theme(&$existing, $type, $theme, $path)
{
$hooks['partners_tio_client_list'] = array(
'template' => 'partners-tio-client-list',
'arguments' => array('form' => NULL, 'role' => NULL),
'path' => drupal_get_path('module', 'partners') . '/theme',
);
return $hooks;
}

// menu hook
function partners_menu()
{
$items['partners/info_man/home/tio'] = array(
'page callback' => 'partners_im_tio_menu_page',
'access arguments' => array('partners information management'),
'type' => MENU_CALLBACK,
);

return $items;
}

function partners_im_tio_menu_page()
{
$output = 'Some stuff';
$output .= theme('partners_tio_client_list');

return $output;
}

// partners_tio_client_list.inc file:
function partners_preprocess_partners_tio_client_list(&$variables)
{
$variables['clientList'] = helper_get_tio_clients($link, $role);
}

// partners-tio-client-list.tpl.php file:

if (isset($variables['clientList'])):

if ($variables['screenType'] == TIO_RESULTS_LINK_TIO_DETAILS):

endif;

// ... code to print out results

Name Area Start Closed Job Coach Client Status Action Plan Updated Active Partner TIO Status

endif;

Comments

Jaypan’s picture

Are any of these functions in a file other than the .module file?

gony’s picture

Yes.

1. The partners.module contains:
a. function partners_theme(&$existing, $type, $theme, $path)
b. function partners_menu()
c. includes_once partners_im_tio_menu_page.inc (see 2. below)
d. includes_once partners_tio_client_list.inc (see 3. below)

2. partners_im_tio_menu_page.inc contains:
c. partners_im_tio_menu_page() which is what is called when users goes to url that is defined in partners_menu()
this calls theme() function and adds result to $output variable (which should trigger 3.d and 4.e- and it works most times):
$output .= theme('partners_tio_client_list');

3. partners_tio_client_list.inc contains:
d. function partners_preprocess_partners_tio_client_list(&$variables)

4. partners-tio-client-list.tpl.php
e. contains the template code to uses $variable[] set in 3.d

Jaypan’s picture

The problem you are seeing generally happens when the file isn't properly included. It gets included right after the cache is rebuilt, but afterwards it fails.

To include files you want to use module_load_include() or form_load_include(), or you need to ensure that the required files are included by adding them to your hook_menu() call. Or, you can move the preprocess function to your .module file.

alberto56’s picture

Thanks to all of you for the collective head scratching. Looking at the code line per line and with a talented colleague, I found that the problem lies completely elsewhere:

My preprocess function contained logic which detected IE7 and displayed a special "Sorry your browser is not supported" popup. For a browser other than IE7, the code was executed as usual.

The problem is that we are using two levels of caching, and if our site is hit by IE7 at the exact moment when the cache needs rebuilding (every half hour), then we are stuck with a broken site for the next half hour.

Two things led to on the wrong path:

First, I was told that it seemed that we were getting a lot more database deadlocks around the time of the problems. It turns out my problems has nothing to do with deadlocks, the theme registry, preprocess hooks, but sometimes we get tunnel vision and focus on something that has nothing to do with our actual problem.

Second, the "Sorry your browser is not supported" popup seems broken, and because no one uses IE7, no one noticed. I _assumed_ the popup was working, so when I did not see the popup I immediately looked for a more complicated explanation.

So the basic take-away here is: never put any code which checks environment-specific information like the browser directly in your template files or preprocess hooks -- as it will be cached!

gony’s picture

Many Thanks I will try that today.

alberto56’s picture

I'm glad I found your post, I'm experiencing the exact same situation. Did you manage to resolve this finally? If so, I would be curious as to what you did. Thanks

Jaypan’s picture

Are any of your processor, preprocessor, or theme functions, not in the .module file?

alberto56’s picture

Yes, they are all in a theme.inc file within my module.

Jaypan’s picture

Then you need to ensure that this file is being included on every page load, or you need to move the functions into your .module file (which is automatically included on every page load). Or, you need to ensure that this file is included before every call you make to theme() that calls one of your theme functions that are in the .inc file.

alberto56’s picture

@Jaypan Thanks, I have moved all my preprocess functions to the .module file.

Jaypan’s picture

Glad that worked. Another way you can do it is to set the 'file' attribute in your hook_theme() definition.

alberto56’s picture

@Jaypan, thanks. The "file" attribute was set in my hook_theme(). The problem was that, intermittently (about once a day on my production server), the file was not included properly, and the preprocess function was not called at all. I have not managed to reproduce this behaviour, so I hope that my preprocess function will always be called now because the .module file is always included.

Cheers,

Albert.

alberto56’s picture

Hmmm, interesting: I moved all my preprocess functions to my main .module file, which is always included. The situation still occurs though: once a day more or less, the preprocess function is simply not being called. It seems that the theme registry sometimes just ignores my preprocess function (note: this occurs only on production and production-like environments, not on MAMP).

OK, my next try will be to put the code of the preprocessor function directly in my tpl.php file, which I know is always included correctly because I can tell from the html code it generates.

Still, this is a workaround, and I would very much like to know why this happens and, especially, how to debug it (perhaps by profiling the php calls after a clear cache)...

Albert

Jaypan’s picture

You shouldn't put your preprocessor function directly in your tpl.php file, as this is most definitely the wrong way to do it.

Actually, looking back at your original code (which was a little hard to read), the problem may be incorrect naming of the preprocessor function.

Is the preprocessor for a theme function you've defined yourself in your module? If so, then this function name:

 function partners_preprocess_partners_tio_client_list(&$variables)

Should be this:

 function template_preprocess_partners_tio_client_list(&$variables)

However if you are preprocessing a theme defined (registered) in a different module, then you are correct in prefixing it with your module name.

alberto56’s picture

Hi,

thanks for taking the time to look at this. Here is my code structure:

- My template is in sites/all/modules/custom/stm_app/theme/stm_app_tabs.tpl.php
- Here is part of my code defined in the sites/all/modules/custom/stm_app.module

// file sites/all/modules/custom/stm_app.module

function stm_app_theme($existing, $type, $theme, $path) {
  $hooks = array(
    'stm_app_tabs' => array(
      'variables' => array(
        'schedule' => NULL, 
      ),
      'template' => '/theme/stm_app_tabs',
    ),
  );

  return $hooks;
}

function template_preprocess_stm_app_tabs(&$variables) {
  // this function gets called most of the time, but on my production
  // server about once every day, it gets ignored. (never called at all).
  // I can simulate the same symptoms by uncommenting the following line:
  // return;
  
  $variables['schedule'] = stm_app_get_schedule();
}

I understand that the preprocessor function should not be put in the tpl.php file. However, what I was thinking was to define the following in my tpl.php file. This would be a last resort if I can't get the preprocessor problem fixed.

$schedule = stm_app_get_schedule();

Cheers,

Albert

alberto56’s picture

Found an issue which might be related: https://drupal.org/node/939462

For now I just got rid of the preprocess function for that particular template, and put all the code in the tpl.php file.

I hope the above link will provide a solution though.

Cheers,

Albert.