Search code examples
drupal-6themescache-control

How should I control drupal cache when switching themes in my module hook_init?


I have implemented a drupal module to toggle two themes according to server time. Its simplified code is:

function toggle_themes_init() {
  global $custom_theme;

  $current_theme = variable_get('theme_default', 'garland');

  // Determine the daytime
  $hours = (int)date('H');

  $new_theme = ($hours >= 8 && $hours < 18 ? 'light_theme' : 'dark_theme');

  // If the default theme differs from $new_theme
  //   then we want to clear the theme cache
  if ($new_theme != $current_theme) {
    variable_set('theme_default', $new_theme);
    drupal_rebuild_theme_registry();
  }

  $custom_theme = $new_theme;
}

I am not sure how to correctly clear the theme cache (for all the pages of the site at once).

Right now theme on some pages don't change (using this code). E.g. theme on page, created by Views module, is changed. While the static page's theme is not.

When I disable the drupal cache everything works ok.

Please give a working advice.

Will be glad for your help!


Solution

  • Try using cache_clear_all().

    It should clear all the caches and make the theme change visible.


    From the Drupal api docs page:
    cache_clear_all($cid = NULL, $table = NULL, $wildcard = FALSE)

    Parameters

    $cid If set, the cache ID to delete. Otherwise, all cache entries that can expire are deleted.

    $table If set, the table $table to delete from. Mandatory argument if $cid is set.

    $wildcard If $wildcard is TRUE, cache IDs starting with $cid are deleted in addition to the exact cache ID specified by $cid. If $wildcard is TRUE and $cid is '*' then the entire table $table is emptied.

    You can use clear_cache_all() and specify arguments to only delete certain cache id's instead of deleting all expirable cache entries.