0
votes

I'm trying to create a simple module to preview themes in Drupal. Each user has their own custom node and I want to be able to show them a preview of how the theme will work without them actually enabling it. Right now this is what I have:

function theme_preview_info($new_theme, $node_id)
{
    global $custom_theme;
    $custom_theme = $new_theme;

    $node = node_load($node_id);

    return $node->body;

}

It will display the content of the node, but the formatting is all messed up. How can I properly display the node exactly as it would as if i went to node/1, but instead view it at theme_preview/theme_name/1?

1

1 Answers

5
votes

Do you have any experience building Drupal modules?

If not, you may find that what you are trying to do is not all that simple. You will need some interface for the user to choose which theme they are previewing. Then, you will need to hook into Drupal's routing to direct users to the appropriate node using the selected theme preview based on a customized path alias (theme_preview/theme_name/1). There's most likely a lot of other back-end overhead that I'm not anticipating at the moment.

If you have experience with building Drupal modules, though, that might not be a big deal.


Switching out the theme is probably the easiest part. If you're in Drupal 7, you can use an implementation of hook_custom_theme() to change the theme used for particular nodes based on your intended criteria (i.e., which theme the user selected).

http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_custom_theme/7

This way, you can actually allow Drupal and the selected theme to completely handle the rendering of the page, giving a more accurate preview.

I'm not sure how exactly you would go about creating the user's theme-switching interface or routing the user to a specific path alias for each theme selection.

You also might want to look into the ThemeKey module (http://drupal.org/project/themekey). It's possible you could use that module somehow to simply set up ab version of each node to be viewed in each different theme.