In the project I am working on I have created a theme hook that renders a .tpl.php file:
/**
* Implements hook_theme().
*/
function my_module_theme() {
$theme['my_module_list'] = array(
'variables' => array(
'#markup' => array(),
),
'path' => drupal_get_path('module', 'my_module'),
'template' => 'templates/my_module_list',
);
return $theme;
}
Then I want some JS and CSS to be added only when this theme hook is used in the current page. Thus I implemented of theme_preprocess_hook() as such:
/**
* Implements template_preprocess().
*/
function my_theme_preprocess_my_module_list(&$vars) {
drupal_add_js(drupal_get_path('module', 'my_module') . '/js/my_module.js');
drupal_add_css(drupal_get_path('module', 'my_module') . '/css/my_module.css');
}
However it appears that the preprocess function is not called every time the hook is used resulting in CSS and JS not being added. What am I missing? Is it bad practice to add it there? And if yes where should I add it in order for it to be loaded only when necessary? I couldn't find any documentation that clarifies it and I've also seen it done other places.
Thanks a lot in advance :-)