1

got a weird issue I was hoping someone could help with.

I have a site that uses multiple subthemes, with different ones being served depending on url var. IE, subsiteone.mysite.com, and subsitetwo.mysite.com get different themes, but the same content.

I am using a module to style each subtheme css via theme settings (https://www.drupal.org/project/style_settings) and am having issues pages caching the wrong stylesheet.

The module setup works fine, rewriting modified css to files/style_settings/subthemeone.css

  • On initial save, all is fine, and page pulls correct (rewritten) files/style_settings/subthemeone.css path.
  • After a while, page caches (?) and reverts css reference to the old (unwritten) sites/all/themes/subthemeone/subthemeone.css

Clearing drupal cache results in the correct (rewritten) reference being called again. Would love to NOT have to resort to disabling caching/etc, so any thoughts appreciated!

EDIT: Using slightly modified code graciously provided below:

style_settings.module

function style_settings_init() {
      setcookie('custom_theme', $GLOBALS['theme'], REQUEST_TIME + 60*60*24*30, ini_get('session.cookie_path'), ini_get('session.cookie_domain'));
}

settings.php

// Check if cookie is set.
if (!empty($_COOKIE['custom_theme'])) {
  // Add theme name to page cache id.
  $_SERVER['REQUEST_URI'] .= '#' . $_COOKIE['custom_theme'];
  // Set Theme.
  $conf['theme_default'] = $_COOKIE['custom_theme'];
  //drupal_set_message($conf['theme_default'], 'status');
}
// Disable page cache if cookie not set so module can set the cookie.
else {
  $conf['cache'] = CACHE_DISABLED;
}
  • Page cache is the issue correct? – mikeytown2 Jun 9 '15 at 15:15
  • Yes, as far as I can tell. When cache is updated (?), it links to css in theme folder, not file folder. And yes, sorry, Page Cache. – Daniel Rogers Jun 9 '15 at 16:27
1

Answer is in here: https://www.drupal.org/node/361832#comment-4204294

You'll need a cookie for the user saying what theme they are using. This is done in a hook_init() call.

  // If no cookie is set, set a cookie with the value of theme_default.
  if (empty($_COOKIE['custom_theme'])) {
    setcookie('custom_theme', $GLOBALS['theme'], REQUEST_TIME + 60*60*24*30, ini_get('session.cookie_path'), ini_get('session.cookie_domain'));
  }

Then you'll need to modify the page cache cid ($_SERVER['REQUEST_URI']) in settings.php

// Check if cookie is set.
if (!empty($_COOKIE['custom_theme'])) {
  // Add theme name to page cache id.
  $_SERVER['REQUEST_URI'] .= '#' . $_COOKIE['custom_theme'];
  // Set Theme.
  $conf['theme_default'] = $_COOKIE['custom_theme'];
}
// Disable page cache if cookie not set so switchtheme can set the cookie.
else {
  $conf['cache'] = CACHE_DISABLED;
}

Code is from D6 but looking it over really quickly it should work for D7.

|improve this answer|||||
  • 1
    thanks a ton @mikeytown2! Works great, only one small hiccup for me it seems. When a user goes to the site for the first time (no cookie) it is still loading wrong sheet. page reload (now has cookie) everything works fine. Editing above question to show my modified code. – Daniel Rogers Jun 10 '15 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.