Drupal hook_nodeapi example - Transform CCK field to body content
This is how you can transform a CCK field and add it to the body content by using nodeapi
<?php
function riddle_nodeapi(&$node, $op, $teaser, $page) {
if ($node->type == "historia") {
switch ($op) {
case 'view':
if ($node->field_svar[0]['value'] != "" AND $page == 1) {
$svar = '<form><fieldset class="collapsible collapsed">
<legend>Svar</legend>
<div>'.$node->field_svar[0]['value'].'</div>
</fieldset></form>';
$node->content['riddle'] = array(
'#value' => $svar,
'#weight' => 0,
);
if (is_array($node->content['field_svar'])) {
unset($node->content['field_svar']);
}
}
break;
}
}
}
?>
In the example above a colapsible field is used and to make this work on a view of a page you have to add the JavaScript collapse.js, witch is normally loaded on add or edit. In my case I added it in the page.tpl.php file in the tag. You find the file in the theme folder.
<?php
<script type="text/javascript" src="/misc/collapse.js?"></script>
?>
Knowledge keywords: