I may be doing something wrong, but I cannot pass basic variables from the a template.php file. The additional variables such as zebra are available, but the majority of variables, eg node->title etc arent available. I have tested by adding

print_r(get_defined_vars());

but other than the custom variables nothing shows up.

Seems to be affecting some others - http://drupal.org/node/25297.

Comments

gollyg’s picture

Priority: Normal » Critical

Bumped this up to critical as it would seem to be a serious problem with the engine (or i have got it completely wrong ;).

adrian’s picture

Priority: Critical » Normal
Status: Active » Closed (works as designed)

this is not critical, and is by design

template.php itself has access to nothing. drupal doesn't have any context, or global variables. The only exception to this rule is the currently logged in global $user.

If you mean the _phptemplate_variables function it gets called with variables in it's $variables array parameter, and the contents of that array is different depending on which theme function is calling it.

The only time a $node object is passed through phptemplate_variables, is if ($hook == 'node') , or if ($hook == 'page') && (page is a node page), and then it is accessed as $variables['node'];

There is a lot of documentation on the phptemplate handbook about how to use this function

http://drupal.org/phptemplate

adrian’s picture

Status: Closed (works as designed) » Active

making this active again.

looking at the link, perhaps you did not explain the bug properly.

adrian’s picture

also

lines 212 to 214 of phptemplate.engine, that handle the node passing, have not changed much

http://cvs.drupal.org/viewcvs/drupal/drupal/themes/engines/phptemplate/p...

gollyg’s picture

Eek! Re-reading my post i realise i described it terribly!

bryan kennedy’s picture

I am having this same problem so maybe I can help explain:

I need the node object (well, the nid part at least) in my page.tpl.php file and so am using this code in the template.php file. It used to work in 4.6 but is giving me a php error in 4.7:

function _phptemplate_variables($hook, $vars) {
   switch($hook) {
     case 'page' :
        $vars['nid'] = $vars['node']->nid
        break;
   }
   return $vars;
}

Has there been a change that stops this from working? If so we need to change the documentation.

adrian’s picture

The node object is passed to page.tpl.php when the url matches the format 'node/nid', then and only then.

The node object is not available on any other pages, at any other time.

You do not need to alias the 'nid' variable, as you can access the $node->nid variable directly from within your page.tpl.php.

The only thing that has changed about this code since it was originally checked into the drupal core repository, is that we
got rid of the node_load(array('nid' => nid) ) formatting, and now use node_load(nid).

radagast-1’s picture

This restriction is a problem for me as I have a skin that requires a sidebar with information about the node owner. I need to know who the node owner is in template.php to do this properly.

Is there a way to route around this problem without hacking the Drupal core code? Basically I need access to $node->uid and $node->name from template.php for all nodes.

thinkingman’s picture

Same here, I would like access to this info. Seems really basic, especially when everything disappears within context of a node.

adrian’s picture

  if ((arg(0) == 'node') && is_numeric(arg(1)) ) {
     $node = node_load(arg(1));
  }

Once again, this is not a bug. this is how Drupal (not phptemplate was designed), to fix this in any meaningful way without a workaround would involve modification of the Drupal API to store the things it retrieves.

discursives’s picture

perhaps if folks here posted some of the code in their template.php file we could see what else we could do on this.

TimG1’s picture

Subscribing. Here's the code that works fine for me in node.tpl but not in template.php.

foreach( (array)$node->taxonomy as $term ) {
    if ($term->tid == 176) {$banner_switch = "yes";}
  }

And here it is again when inside my template.php....

function phptemplate_preprocess_node(&$vars, $hook) {
$function = 'phptemplate_preprocess_node'.'_'. $vars['node']->type;
  if (function_exists($function)) {
   $function(&$vars);
  }  
  return $vars;
}

function phptemplate_preprocess_node_newsletter(&$vars) {
  foreach( (array)$node->taxonomy as $term ) {
    if ($term->tid == 176) {$banner_switch = "yes";}
  }
}

My phptemplate_preprocess_node_newsletter function is working fine. I did the below to test to make sure I could assign a variable and print it out in my node.tpl....

function phptemplate_preprocess_node_newsletter(&$vars) {
  $vars['testVar'] = 'hi';
}