Last updated October 26, 2009. Created by kitt on July 3, 2006.
Edited by esmerel. Log in to edit this page.
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.
Comments
Error In Function: theme_todo_basic_view()
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
hook_viewchanged in Drupal 5.0The first parameter of the function
todo_viewhas 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
For Drupal5
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;
}
?>
Bibek Shrestha
http://www.bibekshrestha.com.np