[development] How to implement hook_nodeapi when op is 'view'?
Dave Cohen
drupal at dave-cohen.com
Mon Jun 18 19:43:08 UTC 2007
In a recent thread I asked about using #pre_render callbacks to affect the way
nodes are rendered. (See http://drupal.org/node/147662). This goes some way
toward solving my problem, but things could be better. When my #pre_render
hook is called, the data structure is filled with arrays that look like:
[SOME KEY HERE] => Array (
[#value] => SOME HTML HERE
[#weight] => SOME NUMBER HERE
)
Because the #value field contains HTML, its very hard to modify. Practically
speaking, all I can do is remove some fields from the node content, or
reorder fields. I can't manipulate each field the way I really want. (For
example, I may want to theme a CCK field differently depending on whether the
node in question is a page or a story.)
I'd like to see developers stop using this #value => HTML model when
implementing hook_nodeapi($op = 'view'). Everyone does, because the
documentation on api.drupal.org suggests implementing it this way:
<code>
case 'view':
$node->content['my_additional_field'] = array(
'#value' => theme('mymodule_my_additional_field', $additional_field),
'#weight' => 10,
);
break;
</code>
Instead, it should be implemented something like:
<code>
case 'view':
// my_additional_data is an array
$node->content['my_additional_data'] = $node->my_additional_data;
$node->content['my_additional_data']['#theme']
= 'mymodule_my_additional_data';
$node->content['my_additional_data']['#weight'] = 10;
break;
</code>
What this does is postpone the theming until drupal_render is called. And
#pre_render hooks have a chance to manipulate data or change the theme
function to be called.
Please let me know if I'm right or wrong about this,
-Dave
More information about the development
mailing list