Looking to make it so that all entries (blogs, event, story, audio, story, etc.) for a given user appear in the users self-selected theme. Can't seem to figure it out. Any help is greatly appreciated.

Comments

marcp’s picture

I haven't tested this, but it's a subset of some stuff that we've used. The key is that you need to set the global $custom_theme variable, and I think it needs to be done pretty early on in the page processing (ie. before any theming functions have been called), so it's important to do it in hook_menu.

Create a new module called user_theme_swapper.module and put this in there:

function user_theme_swapper_menu($may_cache) {
  if (!$may_cache) {
    if ((arg(0) == 'node') && is_numeric(arg(1)) {
      $node = node_load(arg(1));
      $user = user_load(array('uid' => $node->uid));
      if ($user->theme) {
        $theme = $user->theme;
      }
      else {
        $theme = variable_get('theme_default', 'bluemarine');
      }
  
      global $custom_theme;
      $custom_theme = $theme;
    }
  }
}

Then make sure to go to the module administration page and activate the module.

Marc

-------
http://www.funnymonkey.com
Tools for Teachers

marcp’s picture

Whoops, I was missing a paren -- here is an actual working version that has been tested:

function user_theme_swapper_menu($may_cache) {
  if (!$may_cache) {
    if ((arg(0) == 'node') && is_numeric(arg(1))) {
      $node = node_load(arg(1));
      $user = user_load(array('uid' => $node->uid));
      if ($user->theme) {
        $theme = $user->theme;
      }
      else {
        $theme = variable_get('theme_default', 'bluemarine');
      }
 
      global $custom_theme;
      $custom_theme = $theme;
    }
  }
}

Marc
-------
http://www.funnymonkey.com
Tools for Teachers

mrtoner’s picture

Marc, is it possible that something like this might be used so that anyone viewing the author's content would see it in the author's self-selected theme? Better yet, is it possible to use the value of a CCK field to set the theme for viewing? I'd set an selection list with the themes I'd allow for use.

...

Dummy me: the Taxonomy Theme module gives me this ability! I've been using the module for a while on a different project, but didn't realize this feature existed. I've got a couple features to talk to the developer about...

http://drupal.org/project/taxonomy_theme

marcp’s picture

Taxonomy Theme looks good...

The module code I posted above does what you are asking -- all node pages will appear in the theme selected by the author of the node.

Marc

-------
http://www.funnymonkey.com
Tools for Teachers

joetomgo’s picture

I installed the userswaptheme.module and activated it.

The user's blog entries appear to be working, but the blog and user home pages are not.

Thanks for the code though. It's probably simple.

http://community.bangordailynews.com/blog/24 doesn't work, but a specific entry: http://community.bangordailynews.com/node/133 does.

marcp’s picture

function user_theme_swapper_menu($may_cache) {
  if (!$may_cache) {
    $uid = 0;
    if ((arg(0) == 'node') && is_numeric(arg(1))) {
      $node = node_load(arg(1));
      $uid = $node->uid;
    }
    else if ((arg(0) == 'blog') && is_numeric(arg(1))) {
      $uid = arg(1);
    }
    
    if ($uid != 0) {
      $user = user_load(array('uid' => $uid));
      if ($user->theme) {
        $theme = $user->theme;
      }
      else {
        $theme = variable_get('theme_default', 'bluemarine');
      }
 
      global $custom_theme;
      $custom_theme = $theme;
    }
  }
}

-------
http://www.funnymonkey.com
Tools for Teachers

lias’s picture

thanks.

marcp’s picture

Give it a try and let us know... You'll need a .info file, but that should be very easy to create. See the Converting 4.7.x modules to 5.x guide for help if it doesn't work out-of-the-box.

Marc
-------
http://www.funnymonkey.com
Tools for Teachers