07. Load the data for viewing and editing

Last modified: July 8, 2006 - 16:51

In order for the new nodes to display the additional information, Drupal needs to know where the data is. Implementing the load hook will load the additional node information for our new node from the database, allowing us to both display the additional information when displaying our node type, as well as edit the node.

<?php
/**
* Implemenation of hook_load
* @param node object to load additional information for
* @return object with todo fields
*/
function todo_load($node) {
 
$t = db_fetch_object(db_query('SELECT duedate, priority, taskstatus FROM {todo} WHERE nid = %d', $node->nid));
 
 
// adjust the date to human readable format
 
$t->duedate = date('d/m/Y', $t->duedate); 

  return
$t;
}
?>

Because we're displaying a date to the user, but storing a unix timestamp in the database, we need to manipulate the date field to convert it to human readable format after retrieving the data from the database. This is a simple example of what can be done in the load hook.

However, if we adjust the date in the load hook, we prevent all other subsequent methods of viewing from adjusting the display format of our date - we've forced the format to be day-month-year, which may look odd to some people, and confuse others. We still want to convert it to the default format when editing, though.

Fortunately, we have the prepare hook. Prepare hook functions are called after the load function, but before the node is rendered in a submit form view, and is the best place for data manipulations that need to be done before the node is used when adding or editing.

Rewriting our function, using the prepare hook, we get

<?php
/**
* Implemenation of hook_load
* @param node object to load additional information for
* @return object with todo fields
*/
function todo_load($node) {
   
$t = db_fetch_object(db_query('SELECT duedate, priority, taskstatus FROM {todo} WHERE nid = %d', $node->nid));
    return
$t;
}

/**
* Implemenation of hook_prepare
* @param node object to display
*/
function todo_prepare(&$node) {
   
$node->duedate = date('d/m/Y', $node->duedate);
}
?>

More information about the load hook can be found in the hook_load documentation.

More information about the prepare hook can be found in the hook_prepare documentation.

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

the default value gives 01/01/1970 when creating a new todo

yasheshb - October 5, 2007 - 07:21

hello,

i'm using php 5.1.6 / drupal 4.7.7 and when i use the hook_prepare as given above i get
a default value of "01/01/1970" for due date when i create a new todo

http://localhost/drupal-4.7.7/?q=node/add/todo

This is due to the fact that the todo_prepare gets called before the todo_form and
since the duedate is not set it gets an incorrect value of "01/01"1970" which gets
displayed as default value in the hook_form.

the solution to this is to check if duedate is set.

<?php
 
if (isset($node->duedate)) {
   
$node->duedate = date('d/m/Y', $node->duedate);
  }
?>

hope it's useful to others.

rgd.

yashesh
----------------------------------------------------------
Go Pre
http://www2.localaccess.com/rlalonde/pre.htm
----------------------------------------------------------

 
 

Drupal is a registered trademark of Dries Buytaert.