0

During a cron run, I have a module that will cache the markup for many nodes. My problem is that during this cron run, any markup from the render function won't go through my themes hooks or templates.

From within my module code, how can I select the theme? Is there a hook? Is there a function where I can specify it?

Ultimately, I want to be able to do this and get the same results as if I were running this on a page_build hook:

render(node_view($node, 'teaser'));
render(node_view($node, 'mini_teaser'));
curiouser
  • 970
  • 2
  • 12
  • 22
  • 1
    Try adding `global $custom_theme; $custom_theme = 'theme_name';` to the top of your `hook_cron()` implementation – Clive Jan 10 '13 at 18:38
  • That didn't seem to make into Drupal 7, but in finding that API entry from Drupal 6, I found a Drupal 7 hook for setting a theme: http://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_custom_theme/7 – curiouser Jan 10 '13 at 19:01

1 Answers1

0

Drupal 7 has a hook that allows a module to change the currently enabled theme: hook_custom_theme().

Notice that the code used to invoke that hook is the following one. (See menu_get_custom_theme().)

// First allow modules to dynamically set a custom theme for the current
// page. Since we can only have one, the last module to return a valid
// theme takes precedence.
$custom_themes = array_filter(module_invoke_all('custom_theme'), 'drupal_theme_access');
if (!empty($custom_themes)) {
  $custom_theme = array_pop($custom_themes);
}
// If there is a theme callback function for the current page, execute it.
// If this returns a valid theme, it will override any theme that was set
// by a hook_custom_theme() implementation above.
$router_item = menu_get_item();
if (!empty($router_item['access']) && !empty($router_item['theme_callback']) && function_exists($router_item['theme_callback'])) {
  $theme_name = call_user_func_array($router_item['theme_callback'], $router_item['theme_arguments']);
  if (drupal_theme_access($theme_name)) {
    $custom_theme = $theme_name;
  }
}

Since the System module implements that hook, if you implement hook_custom_theme() in a module for which the hook is executed first (e.g. the short name of the module is custom_module), then the System module could override the theme set by your module.

Generally, setting the global $custom_theme should get the same effect. Be sure the theme being set is enabled.

apaderno
  • 28,547
  • 16
  • 75
  • 90