08. Adjust forms for editing

Last modified: July 8, 2006 - 16:57

At this point we can create a node of our new type, save it to the database, reload it from the database, and display it in our edit form (after creating go to the edit url of node/{the new node id}/edit to view the edit form). However, if we try to save an edited version of the node at this point, it won't save.

The workflow for saving a node is slightly different than adding the node. In particular, where the insert hook saves the new node's data, the update hook saves an existing node's data to the database.

For this function, we'll update the database fields as we did with the insert fields. Once again, we need to adjust the date field, converting from human readable format to the unix timestamp.

<?php
/**
* Implementation of hook_update, which saves updated todo-specific
* information into the todo table
* @param node object
*/
function todo_update($node) {

   
// normally, we'd try strtotime, but it won't handle the inverse
    // dd/mm/yyyy formats, so generate the timestamp ourselves
   
preg_match('/^(\d\d)\/(\d\d)\/(\d\d\d\d)$/', $node->duedate, $m);
   
$ts = mktime(0, 0, 0, $m[2], $m[1], $m[3]);
   
   
db_query("UPDATE {todo} set duedate=%d, priority=%d, taskstatus=%d WHERE nid=%d", $ts, $node->priority, $node->taskstatus, $node->nid);
}
?>

At this point, we can add, save, edit and save our new node type. Problem is, when the node displays, only the task title and description, information stored in the node and node_revision tables, central to Drupal's node system, actually display. We want to display our other fields by default. We'll do this next.

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

 
 

Drupal is a registered trademark of Dries Buytaert.