7 nodeapi_example.module nodeapi_example_node_update($node)

Implements hook_node_update().

As an existing node is being updated in the database, we need to do our own database updates.

This hook is called when an existing node has been changed. We can't simply update, since the node may not have a rating saved, thus no database field. So we first check the database for a rating. If there is one, we update it. Otherwise, we call nodeapi_example_node_insert() to create one.

Related topics


nodeapi_example/nodeapi_example.module, line 191
Module implementation for nodeapi_example module.


function nodeapi_example_node_update($node) {
  if (variable_get('nodeapi_example_node_type_' . $node->type, FALSE)) {
    // Check first if this node has a saved rating.
    $rating = db_select('nodeapi_example', 'e')
      ->fields('e', array(
      ->where('e.vid = (:vid)', array(':vid' => $node->vid))

    if ($rating) {
      // Node has been rated before.
        ->fields(array('rating' => $node->nodeapi_example_rating))
        ->condition('vid', $node->vid)
    else {
      // Node was not previously rated, so insert a new rating in database.