09. Different views of your data

Last modified: October 26, 2009 - 20:47

We can now define, add, save, edit and update nodes of the node type todo. However, as mentioned earlier, when viewing nodes of our new node type, only the task title and description display automatically. We need to display the rest of our data.

To render a node with its new node types, we need to implement the view hook. This function will allow us to add our fields to the node content before rendering.

<?php
/**
* Implementation of hook_view, add our node specific information
* @param node object to display
* @param boolean is this a teaser or full node?
* @param boolean is this displaying on its own page
*/
function todo_view(&$node, $teaser = FALSE, $page = FALSE) {
   
$node = node_prepare($node, $teaser);
   
$todo_info = theme('todo_basic_view', $node);
   
$node->body .= $todo_info;
   
$node->teaser .= $todo_info;
}
?>

This function calls node_prepare to process the node through the node's output filters. It then appends the output of the function theme_todo_basic_view.

<?php
/**
* Theme function to display additional node data
* @param node to display
* @return HTML string with additional node information
*/
function theme_todo_basic_view($node) {

 
$priorities = _todo_priorities();
 
$statuses = _todo_priorities();

 
$output = '<div class="todo_view_duedate">';
 
$output .= t('Due date: %duedate', array('%duedate' => format_date($node->duedate, 'custom', 'd/m/Y')));
 
$output .= '</div>';

 
$output .= '<div class="todo_view_priority">';
 
$output .= t('Priority: %priority', array('%priority' => $priorities[$node->priority]));
 
$output .= '</div>';

 
$output .= '<div class="todo_view_taskstatus">';
 
$output .= t('Current status: %taskstatus', array('%taskstatus' => $statuses[$node->taskstatus]));
 
$output .= '</div>';

  return
$output;
}
?>

The node_example.module shows another example of this functionality.

At this point, the node's additional information will display for the node. The view can be changed either with CSS defining the additional classes we added around the content parts, or with a custom node template if your site is using the phptemplate theming engine.

More information about the view hook can be found in the hook_view documentation.

Download the module thus far, and rename to todo.module before saving and enabling in your Drupal installation.

Error In Function: theme_todo_basic_view()

ricangroup - August 15, 2006 - 21:34

There is an error in the function theme_todo_basic_view(). The variable $statuses is calling the function _todo_priorities(). It should be calling the function _todo_statuses(). Here is the correct code below:

<?php
/**
* Theme function to display additional node data
* @param node to display
* @return HTML string with additional node information
*/
function theme_todo_basic_view($node) {

 
$priorities = _todo_priorities();
 
$statuses = _todo_statuses();

 
$output = '<div class="todo_view_duedate">';
 
$output .= t('Due date: %duedate', array('%duedate' => format_date($node->duedate, 'custom', 'd/m/Y')));
 
$output .= '</div>';

 
$output .= '<div class="todo_view_priority">';
 
$output .= t('Priority: %priority', array('%priority' => $priorities[$node->priority]));
 
$output .= '</div>';

 
$output .= '<div class="todo_view_taskstatus">';
 
$output .= t('Current status: %taskstatus', array('%taskstatus' => $statuses[$node->taskstatus]));
 
$output .= '</div>';

  return
$output;
}
?>

Error in function todo_view

Jo Wouters - January 7, 2007 - 23:15

hook_view changed in Drupal 5.0
The first parameter of the function todo_view has to be $node (and not a reference &$node), and at the end of the function we should return $node.
See http://api.drupal.org/api/HEAD/function/hook_view

The correct code since Drupal 5.x is :

<?php
/**
* Implementation of hook_view, add our node specific information
* @param node object to display
* @param boolean is this a teaser or full node?
* @param boolean is this displaying on its own page
*/
function todo_view($node, $teaser = FALSE, $page = FALSE) {
   
$node = node_prepare($node, $teaser);
   
$todo_info = theme('todo_basic_view', $node);
   
$node->body .= $todo_info;
   
$node->teaser .= $todo_info;
    return
$node;
}
?>

According to api docs

bibstha - February 3, 2007 - 06:47

For Drupal5

Return value

$node. The passed $node parameter should be modified as necessary and returned so it can be properly presented. Nodes are prepared for display by assembling a structured array in $node->content, rather than directly manipulating $node->body and $node->teaser. The format of this array is the same used by the Forms API. As with FormAPI arrays, the #weight property can be used to control the relative positions of added elements. If for some reason you need to change the body or teaser returned by node_prepare(), you can modify $node->content['body']['#value']. Not that this will be the un- rendered content. To modify the rendered output, see hook_nodeapi($op = 'alter').

http://api.drupal.org/api/HEAD/function/hook_view

So the code should be

<?php
/**
* Implementation of hook_view, add our node specific information
* @param node object to display
* @param boolean is this a teaser or full node?
* @param boolean is this displaying on its own page
*/
function todo_view($node, $teaser = FALSE, $page = FALSE) {
 
$node = node_prepare($node, $teaser);
 
$todo_info = theme('todo_basic_view', $node);
 
// $node->body .= $todo_info;
  // $node->teaser .= $todo_info;
 
$node->content['body']['#value'] .= $todo_info;
 
$node->content['teaser']['#value'] .= $todo_info;
  return
$node;

}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.