1 node_access_example.module node_access_example_node_update($node)

Implements hook_node_update().

If the record in the node_access_example table already exists, we must update it. If it doesn't exist, we create it.

See also

nodeapi_example.module

Related topics

File

modules/examples/node_access_example/node_access_example.module, line 441
Module file illustrating API-based node access.

Code

function node_access_example_node_update($node) {
  // Find out if there is already a node_access_example record.
  $exists = db_query('SELECT nid FROM {node_access_example} WHERE nid = :nid', 
  array(':nid' => $node->nid))->fetchField();

  // If there is already a record, update it with the new private value.
  if ($exists) {
    $num_updated = db_update('node_access_example')
      ->fields(array(
        'nid' => $node->nid,
        'private' => !empty($node->private) ? 1 : 0,
      ))
      ->condition('nid', $node->nid)
      ->execute();
    backdrop_set_message(
    t("Updated node @nid to set private=@private (@num nodes actually updated)", 
    array(
      '@private' => $node->private,
      '@num' => $num_updated,
      '@nid' => $node->nid,
    )
    )
    );
  }
  // Otherwise, create a new record.
  else {
    node_access_example_node_insert($node);
    backdrop_set_message(t('Inserted new node_access nid=@nid, private=@private', array('@nid' => $node->nid, '@private' => $node->private)));
  }

}