Hi all,

I am currently developing a site where on the landing page a user can kick a button where it will apply 1 of 2 themes until that button is clicked again. The theme must stay even if different pages are loaded.

I have been experimenting with the ThemeKey module (http://drupal.org/project/themekey) which is pretty amazing, but I cannot seem to define a set of rules which will allow this.

Any help would be great and thank you in advance. I am happy to explore other modules if need be but would rather stick to ThemeKey if it is achievable.

Comments

jaypan’s picture

Here's how you can do it:

1) Create a block for your theme switcher:

function my_module_block_info()
{
  $blocks['theme_switcher'] = array
  (
    'info' => t('Theme switcher'),
    'cache' => DRUPAL_CACHE_GLOBAL,
  );
  return $blocks;
}

function my_module_block_view($delta = '')
{
  if($delta == 'theme_switcher')
  {
    $block = array
    (
      'subject' => t('Language Switcher'),
      'content' => array
      (
        'form' => drupal_get_form('language_switcher_form'),
      ),
    );

    return $block;
  }
}

Then you declare the form to switch language:

function language_switcher_form($form, &$form_state)
{
  $form['submit'] = array
  (
    '#type' => 'submit',
    '#value' => t('Change theme'),
  );
  return $form;
}

function language_switcher_form_submit($form, &$form_state)
{
  $themes = array('default_theme_name', 'theme_1', 'theme_2');
  $random = rand(0, count($themes) - 1);
  $_SESSION['custom_theme'] = $themes[$random];
}

Finally, you set the theme to be used in hook_custom_theme:

function my_module_custom_theme()
{
  if(!isset($_SESSION['custom_theme']))
  {
    return 'default_theme_name';
  }

  return $_SESSION['custom_theme'];
}

You can now use your block that has been created, that will add a button with which the user can change the theme. A random theme will be applied and will be used until the button is pressed again.

Note that default_theme_name, theme_1, and theme_2 must all be enabled for this to work.


Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
sharsant’s picture

Hi Jaypan,

Thank you very much this will be very helpful!

One question though (and pardon the ignorance), but where am I meant to be placing this PHP code?

If I am correct I cannot place PHP directly into a block via Structure > Blocks > Add Block; So do I have to edit a php file directly?

Thank you again for all your help, I really appreciate it!

jaypan’s picture

You need to put it in a custom module. Have a read on how to create Drupal 7 modules: http://drupal.org/node/361112

You'll need to change 'my_module' in all the code I showed you, to your module key (machine name). You'll understand what this means when you read about hooks.


Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
sharsant’s picture

Excellent, thank you very much. You have helped greatly!

sharsant’s picture

One last question

How can I change the button to an image that behaves like a button?

Again sorry for the ignorance on my behalf. This is really the first I have worked with PHP (although I did find your instructions to be very easy to follow...worked first time for me!)

jaypan’s picture

Well, that's a different topic now isn't it :)

You can mark this one [solved] by editing the original post and adding [solved] to the title.

Glad it worked out for you!


Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
sharsant’s picture

Fare enough ;)

Think I have sorted it any way. Easier than I thought.

Once again, thank you very much for your help!