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

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:

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'];
  }
}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sun’s picture

Priority: Critical » Normal

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

frankcarey’s picture

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

-Thanks

christefano’s picture

@TextPlease: try Enabled modules.

sun’s picture

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';
Agileware’s picture

Same problem, updated code works for me.

Agileware’s picture

FileSize
681 bytes

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

Agileware’s picture

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.
Agileware’s picture

Status: Needs work » Reviewed & tested by the community
sun’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
967 bytes

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

Agileware’s picture

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.

sun’s picture

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).

sun’s picture

Status: Needs review » Fixed
FileSize
3.96 KB
4.75 KB

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

Status: Fixed » Closed (fixed)

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