0

I'm using Theme Key https://www.drupal.org/project/themekey to switch automatically between themes dependent on browser and/or OS id. Now I would like to allow the user to explicitly switch to or from to one or the other theme by clicking a link too. ThePersistant URL https://www.drupal.org/project/purl module might be helpful for this but how exactly can I go about implementing this?

EDIT 1

I followed Answer 1, added drupal_add_library('system', 'jquery.cookie'); into the already existing function marinelli_preprocess_page(&$vars) in my template.php.

I setup two ThemeKey rules like:

system:cookie = "theme=marinelli" THEME [Marinelli] 

and

system:cookie = "theme=skeleton" THEME [Skeleton]

and added following html to my page:

<p><a href="#" onclick="$.cookie('theme', 'marinelli')">Switch to Marinelli</a> <a href="#" onclick="$.cookie('theme', 'skeleton')">Switch to Skeleton</a></p>

But clicking the links, doesn't do anything, seems like I might be missing something or it might not quite work this way because href="#" doesn't actually reload the page if that was the actual idea...?

  • Are you going to use different domains or subdomains for each theme? If not, and you have any caching enabled, you could run into problems where the wrong theme gets cached unintentionally. – Patrick Ryan Aug 23 '14 at 18:50
  • @PatrickRyan there was no intention to use different urls - if I have to, I can just manually link to one or the other but then the usage of Theme Key becomes obsolete, doesn't it? – stdcerr Aug 23 '14 at 18:55
  • 1
    Well, using theme key or not - you will most definitely have caching problems if you have theme switching ability for anonymous users and have page caching enabled. Your options are to disallow theme switching for anonymous users or disable page caching. If either of those are an option I can post an answer for you. – Patrick Ryan Aug 23 '14 at 19:02
  • @PatrickRyan I will be able to disable caching. Thanks for providing an answer! Alternatively, will I be able to use the Devel module and set "Rebuild the theme registry on every page load"? Will that resolve the caching issue too? – stdcerr Aug 23 '14 at 19:23
  • You definitely don't want to do that on a production site – Patrick Ryan Aug 23 '14 at 19:38
1

To make it to work with cache you can use the Mobile Cache module, at least as inspiration to make your own module using the library Mobile Detect.

https://www.drupal.org/sandbox/acy76/1962696

But you need to modify this line in the module and must look like this

_mobile_cache_add_query_string('device=' . $data['device'] . '&theme=' . $_COOKIE['theme']);

Notice that the idea is add the theme name to the cache

I'm using Authcache and Filecache

Thanks @PatrickRyan

| improve this answer | |
1

One way would be to nest each theme switching rule inside another rule using

system:cookie = theme=theme1

     Theme switching rule here

system:cookie = theme=theme2

     Another theme switching rule here

You will need to set up your theme switching links to create a cookie using your method of choice, PHP or jquery.cookie

An example using jquery.cookie could be something like:

template.php:

function THEMENAME_preprocess_page(&$vars) {
  drupal_add_library('system', 'jquery.cookie');
}

Your theme switching links:

<a href="#" onclick="jQuery.cookie('theme', 'theme1'); location.reload(); return false;">Switch to Theme1</a>
<a href="#" onclick="jQuery.cookie('theme', 'theme2'); location.reload(); return false;">Switch to Theme2</a>

Keep in mind that this is not page caching friendly. If you want this to work for anonymous users, you will need to disable page caching.

| improve this answer | |
  • Is it enough to disable caching under admin/config/development/performance? – stdcerr Aug 24 '14 at 3:52
  • Is there a global template.php that gets processed in any case (for any theme)? Or shall the drupal_add_library() go into each's page.tpl.php template e.g.? – stdcerr Aug 24 '14 at 4:03
  • To answer my own question, I found this: api.drupal.org/api/drupal/includes%21theme.inc/function/… - a default page.tpl.php is located in modules/system/page.tpl.php and I believe THEMENAME_preprocess_page should be template_preprocess_page instead, is this right @PatrickRyan? – stdcerr Aug 24 '14 at 4:18
  • Yes that is how you disable the built in caching. All themes have a template.php file usually at sites/all/themes/yourtheme/template.php. This is where the above function goes, just replace THEMENAME with your themes machine name. – Patrick Ryan Aug 24 '14 at 16:51
  • Please see EDIT 1 above – stdcerr Aug 27 '14 at 13:38

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.