Switch theme via URL - doesn't switch until next page load for logged in users only.

frankcarey - June 23, 2008 - 18:28
Project:Switchtheme
Version:5.x-1.2
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:closed
Description

This worked fine with anon users.

After much frustration, it seems something happens when user_save() is called so that changing $user->theme doesn't immediately change the displayed theme. I moved $user->theme = $form_values['theme']; out of the if statement and put it BEFORE user_save() and it worked.

I'm still a little confused as to why, but this change fixed our problem... I'll let another investigate further.

I changed the following code

<?php
function switchtheme_switch_form_submit($form_id, $form_values) {
  global
$user;

  if (
$user->uid > 0) {
   
// Save the setting in the db for logged in users.
    // We do not validate the input here, because that is done in init_theme()
    // already.
   
if (user_save($user, array('theme' => $form_values['theme']))) {
     
$user->theme = $form_values['theme'];
    }
   
$_SESSION['custom_theme'] = $form_values['theme'];
  }
  elseif (
user_access('switch theme')) {
   
// Save the setting in the session for anonymous users.
   
$_SESSION['custom_theme'] = $form_values['theme'];
  }
}
?>

TO:

<?php
function switchtheme_switch_form_submit($form_id, $form_values) {
  global
$user;

  if (
$user->uid > 0) {
   
// Save the setting in the db for logged in users.
    // We do not validate the input here, because that is done in init_theme()
    // already.
   
$user->theme = $form_values['theme'];
   
user_save($user, array('theme' => $form_values['theme']));
   
$_SESSION['custom_theme'] = $form_values['theme'];
  }
  elseif (
user_access('switch theme')) {
   
// Save the setting in the session for anonymous users.
   
$_SESSION['custom_theme'] = $form_values['theme'];
  }
}
?>

#1

sun - June 23, 2008 - 23:04
Priority:critical» normal

Could you please compile a list of other contrib modules you have enabled?

#2

frankcarey - July 1, 2008 - 13:31

we have a lot :) is there an easy way to do that? module perhaps?

-Thanks

#3

christefano - July 29, 2008 - 05:48

@TextPlease: try Enabled modules.

#4

sun - July 29, 2008 - 15:27

meh... installing a module for this purpose is quite a big overhead - just execute this query:

SELECT name FROM system WHERE status = 1 AND type = 'module';

#5

Justin W Freeman - October 21, 2008 - 00:06

Same problem, updated code works for me.

#6

Justin W Freeman - October 21, 2008 - 00:10

Here's a patch with the code to make life easier.

AttachmentSize
switchtheme.module.patch 681 bytes

#7

Justin W Freeman - October 21, 2008 - 00:11
Title:Switch theme via URL - doesn't swith until next page load for logged in users only.» Switch theme via URL - doesn't switch until next page load for logged in users only.

#8

Justin W Freeman - October 21, 2008 - 00:11
Status:needs work» reviewed & tested by the community

#9

sun - October 23, 2008 - 14:10
Status:reviewed & tested by the community» needs review

Well. Basically, attached patch should be the cleanest fix. However, untested - and please do not test this one on production sites.

AttachmentSize
switchtheme-DRUPAL-6--1.user_.patch 967 bytes

#10

Justin W Freeman - October 23, 2008 - 19:04
Status:needs review» reviewed & tested by the community

I've checked the Drupal 6 patch and it's the same as the Drupal 5 patch. We've got the Drupal 5 patch working on a production website and I can confirm it works.

#11

sun - October 24, 2008 - 07:49
Status:reviewed & tested by the community» needs review

What means you "checked the Drupal 6 patch"? Actually it's not the same. That's why I was asking for testing (again, due to changes in the patch on test sites only, please).

#12

sun - November 2, 2008 - 13:34
Status:needs review» fixed

Tested this myself; fixed some more glitches, and committed attached patch(es) to all branches.

AttachmentSize
switchtheme-DRUPAL-6--1.user_.patch 4.75 KB
switchtheme-DRUPAL-5.user_.patch 3.96 KB

#13

System Message - November 16, 2008 - 14:02
Status:fixed» closed

Automatically closed -- issue fixed for two weeks with no activity.

 
 

Drupal is a registered trademark of Dries Buytaert.