How to customize $content variable for Nodes in Drupal
Hello folks
If you have been coding for a while Drupal, you noticed it's hard trying to modify elements inside of a variable $content from template node.tpl.php. Well here's a solution.
Imagine you have a content type, and you want to sort the content type to present two cck fields above the title and before the body, just like the scheme represented below:
CCK Field # 1
CCK Field # 2
Node Title
Node Body
1. Hide the title
The problem is that by default the page.tpl.php prints the title on top of page, so the first thing we need to do is to update the template page.tpl.php to hide the title for our specific content type, lets say the "page" content type. Use the code below in your template:
type != 'page' && $title): print ''. $title .''; endif; ?>
'.>
2. Insert your title before the body
You must create a custom module to implement the hook_nodeapi to alter the content array before this is processed by Drupal. Use the code listed below:
function ensequence_functions_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'view':
if($node->type == 'page'){
$node->content['title'] = array(
'#value' => l($node->title,$node->path) ,
'#weight' => 0
);
}
break;
}
}
Enjoy it.
enzo







