Hey Everyone -
Is it possible / good form to override theme_preprocess_page from within a module? What I'm trying to do in my module is check if there is a parameter in the URL, and if so load a different page theme (stored in the modules directory).

While I figured out how to do this in my theme file, I would like to move the code to a module so it is "template agnostic". I'm guessing something along the following is what I want - hopefully someone can chime in and make sure I'm headed in the right direction?


function phptemplate_preprocess_page(&$variables) {
$path = drupal_get_path('module', 'mycustommodule') .'/theme';

  if ($variables['node']->type == "special-node") {
    if ($_GET['special-template']) {
          $variables['template_files'][] = $path.'/page-node-special';
    }
  }
}

?>

Thanks!

Comments

jaypan’s picture

function [modulename]_preprocess_page() should work I believe. I've used it with preprocessing comments in a module before.

Contact me to contract me for D7 -> D10/11 migrations.

newmediaist’s picture

I'll give that a try right now - thanks!

newmediaist’s picture

ok, [modulename]_preprocess_page(), but the node object doesn't seem to get loaded into $variables, so I can detect what node type is being shown... is there another more appropriate hook maybe which will return the nodetype?

aloyr’s picture

try the hook_preprocess_node(). it outputs all sorts of cool things on $variables.

here are the first few lines of one of my sites:

Array
(
    [nid] => 217
    [type] => page
    [language] =>
    [uid] => 8
    [status] => 1
    [created] => 1236888850
    [changed] => 1260911310
    [comment] => 0
    [promote] => 0
    [moderate] => 0
    [sticky] => 0
    [tnid] => 0
    [translate] => 0
    [vid] => 5138
    [revision_uid] => 66
    [title] => Wellness
    [body] =>  

notice the [type] in there? that looks like what you are looking for.

perl -e 'print unpack(u,";8F4@=&AE(&-H86YG92P@=7-E(&1R=7!A;\"\$*");'