Posted by: Paul on: May 28, 2009
It has somewhat of a steep learning curve, but Drupal is amazing for allowing you to build flexible, complex websites with lots of functionality in very little time.
For a site I’m working on, we had several custom content types coming through the default node template (node.tpl.php) in our customised theme.
This was fine and dandy for quick development, but then the time came to customise the node page for each content type.
First things first, you need a custom node template. Save the node.tpl.php file as node-[your node name].tpl.php and you’ve created a custom node template. (This means if your node type is ‘audio’, you’d save the file as node-audio.tpl.php)
Now we need to customise the template to suit our needs.
If you dive into a standard node template you’ll see something (unhelpful) like this where the content goes:
<div class=”content”>
print($content)
</div>
Um, OK. So how do you get at the functions which compile the content before it gets to this stage? Well, you probably can, but the easier way is to avoid this step and just access the default $node object which drupal gives you access to with each ‘page’.
To find out what’s hiding inside the $node object, simply do something like this:
print_r($node);
(Yes, I like print_r())
The resulting cacophony in your page source will show you all the node attributes and properties you have access to. Chances are you’ll be able to access these directly to create the customised node template you need.
There seems to be an awful lot of duplication in CCK arrays that are produced from the print_r statment, how do you know which one to use.
[#item] => Array
(
[value] => Keppoch Street
[safe] => Keppoch Street
[#delta] => 0
)
bit further down it says
[#children] => Keppoch Street
and again
[#children] => Keppoch Street
under a different part of the array.
Why is it repeated so many times?
just wanted to second (third) the thanks, this is what I needed to move on from my stuck-ness.
Sorry but I am pretty new to Drupal still and I only followed the first half of your post.
Where do you put the “print_r($node);” to show the node object?
I have a custom event content-type that I want to filter for a calendar.
Sometime you’ll need to replace $node->content['field_name']['value'] , with $node->content['field_name']['view'] . That will keep any formatting (such as line breaks) intact. Took me a while to figure that one out… hope it helps.
Thanks Daniel,
Yep, you’re right – the exact part of the $node object best to use depends upon the need – especially when CCK is in the mix
![]()
Paul
This is a great post. I’m new to Drupal and have used this method to customize the output of one of my custom content types. Combined with CCK this is very helpful.
However, I’ve been wondering, is this the “correct” way to achieve this. Once I do this, I am tied to the theme I modified, correct? If I change themes, I need to copy my node-custom.tpl.php file to the new theme.
It seems like there should be a way to tie a node template to a specific content type, rather than a specific content type in a specific template. Any thoughts?
I’ve tried looking through the entire $node->content object to try and reference a field using
$node->content['field_regeventday']['#value']
$node->content['field_regeventday']['#view']
$node->content['field_regeventday'][0]['value']
$node->content['field_regeventday'][0]['view']
all don’t really work. I’m not entirely sure why they don’t work. Maybe i’ve got the path of the field reference wrong but the only one I managed to get working is
$node->field_regeventday[0]['view'];
the first part is referenced by object then continues on with array. Why are all the CCKs wrapped in a [0] tho?
Hi there,
can you help me please, i am new for drupal and i want to design my own custom module. Now i have problem creating my node-modulename.tpl.php file. I know it is for appearance of my new page look but to code it, I don’t know where to start.
thanks for your help.
June 5, 2009 at 7:57 am
Thank you so much for this post…this was exactly what I needed to create custom templates. I appreciate your effort!