You are here

hook_node_view (D7)

Seems basic but the part that is not in black and white when searching this hook is how to set up the theme render element.
Below is about all the help I could find, but looking into the field.module I was able to figure out how to make a theme function for my custom element.

<?php
function hook_node_view($node, $view_mode, $langcode) {
$node->content['my_additional_field'] = array(
'#markup' => $additional_field,
'#weight' => 10,
'#theme' => 'mymodule_my_additional_field',
);
}
?>

To continue with the example provided lets add in the theme hook.

<?php
function hook_theme() {
$theme = array(
'mymodule_my_additional_field' => array(
'render element' => 'element',
),
return $theme;
}
?>


<?php
function mymodule_my_additional_field($element) {
$output = '<div class="field">';
$output .= '<div class="field-label">'. $element['element']['#title'] .'</div>';
$output .= '<div class="field-item even">'. $element['element']['#markup'] .'</div>';
$output .= '</div>';
return $output;
}
?>

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.