views:

36

answers:

2

hi,

I need to modify the node content Array before it is rendered into html. For this reason I cannot use the $content variable in my node template. I'm looking for it in template.php file, but I cannot find it.

thanks

A: 

Original function that generates variables available to the node is: http://api.drupal.org/api/function/template_preprocess_node/6

You can modify template variables by implementing your own node preprocess function inside template.php that will execute after original function therefore allowing you to add your own variables:

function phptemplate_preprocess_node(&$vars, $hook) {
   // Here you can add your custom variable...
   $vars['myContent'] = "something";
}
bas
hi, thanks for reply. What I actually want is the structured tree with the content. I noticed that the content is stored into $vars['content'] as html. So it is already rendered there and I cannot access to the children (CCK fields).
Patrick
you are having raw node data inside $vars['node'] variable. To check whats there, just do var_dump($vars); inside preprocess function and and look at the page source.
bas
if I replace the content in $vars['node'], is itautomatically replaced in the node as well ? I don't understand why it is redundant, I have both $vars['content'] and $vars['node'].
Patrick
+1  A: 

AFAIK, you can not access the unrendered node content array from within a theme, as the theme processing occurs to late in the processing cycle (i.e. the content array will already be rendered as you observed).

The standard way to access and modify the node content array before it gets rendered would be to implement hook_nodeapi() within a custom module, reacting to the 'view' operation. This gets invoked after the content array has been assembled, but before it gets rendered, allowing you to adjust it at will.

Be aware that other modules might do this as well - if that is the case and you want to adjust values provided by other modules, the call order of the modules becomes relevant and you might need to adjust your modules weight to ensure it gets called after the others.

Henrik Opel
hi, so I have added to a custom module the function: function hook_nodeapi( die(); } but nothing happens. I have refreshed the cache. Thanks
Patrick
Henrik Opel
yeah thanks.. i just found out now!
Patrick

related questions