Hi all. What I am wanting to do is make the display of the node title conditional on the node type in a phptemplate theme. The only stumbling block is that information about the node type is not available within page.tpl.php. The node object does not exist within _phptemplate_callback() either, so I am unable to use _phptemplate_variables() to add the type information to vars[]. Is there another way to get access to the node type?

I suppose I could create a new module with it's own template but that seems like overkill.

Comments

gordon’s picture

The node object is exported to the page template. It is set up in the phptemplate_node() and not as apart of _phptemplate_callback().

You should be able to access it via the $node variable.
--
Gordon Heydon
Heydon Consulting

--
Gordon Heydon

ljs’s picture

The result of gettype($node) within the page template is NULL. From what I can see $node never makes it into phptemplate_page(). I can access $node from within the node template but that doesn't accomplish what I was wanting to do, which was make the display of the node title (when viewing the complete node) conditional upon the node type.

gordon’s picture

check line 252 of phptemplate.engine in the phptemplate_node(). it has been there for ages.

  $vars = array(
      'title'          => $node->title,
      'node_url'       => url('node/' . $node->nid),
      'terms'          => theme('links',$taxonomy),
      'name'           => format_name($node),
      'date'           => format_date($node->created),
      'sticky'         => $node->sticky,
      'picture'        => theme_get_setting('toggle_node_user_picture') ? theme('user_picture', $node) : '',
      'content'        => ($main && $node->teaser) ? $node->teaser : $node->body,
      'links'          => $node->links ? theme('links', $node->links) : '',
      'mission'        => $mission,
      'page'           => $page,
      'taxonomy'       => $taxonomy,

    /* Lastly , pass the actual node to allow more customization */
      'node'           => $node,
      'main'           => $main,
      'page'           => $page
    );

Also which version are you running? it could be that.
--
Gordon Heydon
Heydon Consulting

--
Gordon Heydon

gordon’s picture

What ever I was smoking it must have been good. You are wanting to get access to the node type in the page.tpl.php.

This is not available, but what you can do is use the arg() to get the node id from $_GET["q"] which will then allow you to use node_load() to load the node that you are going to display.

The only problem is that you need to check that you are displaying a node before you try and load one.
--
Gordon Heydon
Heydon Consulting

--
Gordon Heydon

ljs’s picture

Yeah that sounds like what i'm looking for. I'll play with that and see where it goes.

media girl’s picture

If you would, post your results. This is something that would be very handy for a couple of projects I'm working on, and a workable hack or module would be much appreciated by me.
--
mediagirl.org

gordon’s picture

Well you could do something like this.

if (arg(0) == 'node') {
  $node = node_load(array('nid' => arg(1)));
}
else {
  $node = NULL;
}

Be warned, I have not tested this.
--
Gordon Heydon
Heydon Consulting

--
Gordon Heydon

ljs’s picture

I get back from the weekend and you've done my homework for me! That snippet seems to work fine. Thankyou :)

freyquency’s picture

$node->type

Should be able to get the node type in your node.tpl.php. Then you should be able to create IF/ELSE statements that control it. For example:

if($node->type == "blog") {
print $title;
}

Of course you probably want to use it with the alternate syntax, as that is the method that phptemplate uses and allows for better html formatting. for example:

<?php if($node->type == "page"): ?>
<h2>$title</h2>
<?php endif ?>

I haven't actually tested it but use $node->type to output which type it is and add it to the nodes container for specific styling.

Hope that helps!

this post brought to you by:
`':,..,:'``':,..,:'``':,..,:'``':,..,:'`
erik mallinson

ljs’s picture

Yeah that's what I'm doing, except that there doesn't seem to be a node object for me to test.

freyquency’s picture

Are you trying it in page.tpl.php or node.tpl.php? I don't think it would work in page.tpl.php because you would need it to be suited to each time a node is displayed, which is when node.tpl.php is called.

this post brought to you by:
`':,..,:'``':,..,:'``':,..,:'``':,..,:'`
erik mallinson

ljs’s picture

I had the order of template processing arse about, so kinda fixed. Thx both of you. Looks like I'll have to create a new module to do what i want anyway.

chx’s picture

See http://drupal.org/node/11811 and apply it to theme_node, ie. make _phptemplate_node with appropriate parameters. You will get node and you may do whatever you wish here before the callback.

--
Drupal development: making the world better, one patch at a time. | A bedroom without a teddy is like a face without a smile.

dublin drupaller’s picture

Hey guys..

A trying to get the $node->type available for page.tlp.php as well. I clicked through to the links given, but can't make head or tail of the instructions.

Anyone out there with a simple pointer on how to make the variable available with page.tpl.php?

Thanks in advance.

Dub

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

Mateo’s picture

As mentioned above, put this in your code (at the beginning)

if (arg(0) == 'node') {
  $node = node_load(array('nid' => arg(1)));
}
else {
  $node = NULL;
}

The logic behind this is: If the first arguement of the page (IOW, with http://www.yoursite.com/node/123, "node" is arg(0)) is equal to "node" it will call the function "node_load" and pass the array to the variable $node where the node ID is equal to the first arguement ("123" in my example).

Bsaically, load the above code before you want to play with the node information in the page.tpl.php file.

Mateo

Mateo’s picture

If you only want the one variable (type) you might want to do this instead as it is makes fewer SQL calls.

if arg(0)=="node" {
  $type = db_query("Select n.type from {node} n where n.nid = ". arg(1));
}
else {
  $type = NULL;
} 

My php isn't the best, but I think that will work. I would try it myself first, but I'm supposed to be working :)

The end result will give you a variable named "$type" which will tell you the node type on each page load. Use $type anywhere you would normally use $node->type. Realize, however, that you're adding an extra SQL query to each page load, but it's better than querying the entire node content if you are only planning on using the node type.

Mateo

aaron’s picture

I believe that node_load stores the data statically, so it would only call it once, and it would actually be less of a strain on the database to use node_load than a whole new SQL call.

- Aaron

Culture Fix Web Identity & Design
Digital Folk Art (my blog)

sime’s picture

Am I right to say that $node is now available in page.tpl.php as of 4.7?

From an ecommerce perspective, I can also use $node->ptype (cool).

.s

Hangya’s picture

it seems to be working, but I can't use the $user object :( I want to create the template different for logged-in and guest users... Any idea how to do this?

nevets’s picture

To use the $user variable you will need to first declare it as a global like this

global $user;
Hangya’s picture

Thanks :)

dvessel’s picture

Did something change in the latest release? (4.7.2)

All I did was add this to my body class=.. in the page.tpl.php file.

if ($node->type) { print "b-type-" . ($node->type); }

Prints out fine when the node is in full view and hides elsewhere.

–joon park
http://www.dvessel.com

joe.murray’s picture

Interestingly, in 4.6 I get inconsistent results when trying to reference $node->type in page.tpl.php: on some nodes I get it and on others I don't.

Here's the code I'm using to get the $tid for the current node in order to display a particular image:

/* get category tid for this page */
	if (arg(0)=='node') { 
	  /* lookup issue category given nid */
  	  if (strlen(arg(1)) <= 0) {
	  	$this_tid = NULL;
		} else
		{
			$ssql = db_rewrite_sql("SELECT DISTINCT td.tid FROM {term_data} AS td INNER JOIN {term_node} AS tn USING (tid) INNER JOIN {node} AS n WHERE td.vid=1 AND tn.nid=".arg(1)." AND n.type='flexinode-11';");
		  $result = db_query($ssql);
		  if($row = db_fetch_object($result)) {
		    $this_tid = $row->tid;  
		  } else {
		    $this_tid = NULL;	
	  	  }
		}
	} else {
		$this_tid = NULL;
	}
	$output = "";
	if ($this_tid != NULL) {

		/* display issue image without labels for this page's category */

	}
	print $output;

Joe Murray

Joe

heine’s picture

AND tn.nid=".arg(1)."

That's an amazing potential for a security hole you have there Joe. See http://drupal.org/node/62304

Use is_numeric to test for a nid. And don't paste user data in sql queries.

--
The Manual | Troubleshooting FAQ | Tips for posting | Make Backups! | Consider creating a Test site.

ajwwong’s picture

Thanks for the snippet here, guys... great stuff...

http://drupal.org/node/17777#comment-32105

I used this because I was trying to turn off TinyMCE for particular node types, but I didn't know how to do it. But this helped me get things sorted.

Best,

Albert
Esalen Alumni Group

onelittleant’s picture

If you want the full node object available in page.tpl.php you can get it without exposing yourself to security issues or additional unnecessary database hits.

Insert the following at the top of node.tpl.php:

<?php
global $mynode;

if($page) {
  $mynode = $node;
}
?>

Then, in page.tpl.php:

<?php
global $mynode;

if($mynode != null)
{
  // Do some stuff with the node data.
}

?>

This simply creates a global handle on the node that has already been loaded and rendered by Drupal. You can access it similarly from anywhere in your template architecture... anywhere that is called after theme_node anyway...

nevets’s picture

Given this is an old thread it should be pointed out with Drupal 5 $node is directly available in page.tpl.php when viewing a single node.

Izz ad-Din’s picture

What about situations were arg(1) isnt availabe as nid e.g. the node creaTion form?