How hook_node_view works in drupal 7

function hook_node_view

7.x node.api.php hook_node_view($node, $view_mode, $langcode)

Act on a node that is being assembled before rendering.

The module may add elements to $node->content prior to rendering. This hook will be called after hook_view(). The structure of $node->content is a renderable array as expected by drupal_render().

Parameters

$node: The node that is being assembled for rendering.

$view_mode: The $view_mode parameter from node_view().

$langcode: The language code used for rendering.

Example of drupal 7 hook_node_view

Hi there, this took me a bit of searching as well!

After reading comments from this thread and others, I figured it out – here is what I did:

1) In my custom module called "Assignment" I added this:

function assignment_node_view($node, $view_mode) { if ($node->type=="assignment"){ if ($view_mode == 'full'){ $node->content['my_extra_content'] = array( "#markup"=> '
My extra content!
', "#weight"=> 100 ); } return $node; } }

2) then in node–assignment.tpl.php, I added this:

if (array_key_exists("my_extra_content",$content)){ print drupal_render($content["my_extra_content"]); } 

And that worked!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

WordPress.com.

Up ↑

%d bloggers like this: