Hello,

I would like to switch the theme when a condition is satisfied (is mobile).
The problem is that when the cache is enabled, the condition is ignored.

Thank you in advance!

Comments

djg_tram’s picture

Dear me. That must be a Drupal 7.14 issue, I still have a production site with 7.12 where it works flawlessly, so I had no suspicion. I'll look into it.

djg_tram’s picture

The plot thickens, see http://api.drupal.org/api/function/hook_custom_theme/7. The second paragraph has a warning about it not working with page cache. I don't know when this text was added but I can't recall having seen it earlier when I ported the module. I'm not yet sure how we can work around this, if it's possible at all.

Clément’s picture

Thank you for your quick response!

It's annoying because no cache Drupal is really slow!

The solution might be to disable the cache when the condition is required to switch theme?

Clément’s picture

I found a solution!

(with module : mobile theme)

djg_tram’s picture

Assigned: Unassigned » djg_tram

Very nice catch, this seems to work all right, yes, although I only had a quick test. All it takes (besides installing Dynamic Cache, of course) is to add this to the end of virtual_site_theme.module:

/**
 * Implements hook_boot().
 *
 * We need Dynamic Cache (http://drupal.org/project/dynamic_cache) to make sure we can disable caching from here.
 */
function virtual_site_theme_boot() {
  global $conf;

  require_once DRUPAL_ROOT .'/sites/all/modules/virtual_site/virtual_site.module';
  if ($site = virtual_site_load_active()) {
    if (isset($site->features['virtual_site_theme_feature']['theme'])) {
      $conf['cache'] = FALSE;
      drupal_page_is_cacheable(FALSE);
    }
  }
}

If you try it and you also find it OK, I'll put it into dev immediately.

Clément’s picture

Hello, I'm sorry but for me it does not work when de cache is enabled.

virtual_site_load_active() return false when it is tested

I just tested this:

Condition OK 1 == display
Condition OK 2 == hidden

/**
 * Implements hook_boot().
 *
 * We need Dynamic Cache (http://drupal.org/project/dynamic_cache) to make sure we can disable caching from here.
 */
function virtual_site_theme_boot() {
  global $conf;

  require_once DRUPAL_ROOT .'/sites/all/modules/virtual_site/virtual_site.module';
  echo 'Condition OK 1';
  if ($site = virtual_site_load_active()) {
    echo 'Condition OK 2';
    if (isset($site->features['virtual_site_theme_feature']['theme'])) {
      $conf['cache'] = FALSE;
      drupal_page_is_cacheable(FALSE);
    }
  }
}
djg_tram’s picture

It worked for me when I changed it but then stopped swapping the theme, yes. Needs more thought, unfortunately.

adamps’s picture

Assigned: djg_tram » adamps
Issue summary: View changes

I don't have enough knowledge to say if there is a way to get Drupal cache to be aware of caching separately for different Virtual Sites. Possibly it is already able to do it for different languages and the same technique could be used.

However I can suggest a workaround - it's quite common to see websites with a different URL for mobile, which would presumably solve your problem. Or, now 3 years on from the original issue, I'd say responsive themes are gradually taking over from separate mobile sites.

I will try to write some documentation for this, that's probably all we can do.

adamps’s picture

In later email discussion djg_tram pointed out another module that could help, ThemeKey https://www.drupal.org/project/themekey which claims:

And unlike other theme switching modules, ThemeKey should play well with internal and external page caches, like Boost or Varnish, even for anonymous users.