You are here

function node_access_example_node_update in Examples for Developers 7

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

node_access_example/node_access_example.module, line 448
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();
    drupal_set_message(t("Updated node @nid to set private=@private (@num nodes actually updated)", array(
      '@private' => $node->private,
      '@num' => $num_updated,
      '@nid' => $node->nid,
    )));
  }
  else {
    node_access_example_node_insert($node);
    drupal_set_message(t('Inserted new node_access nid=@nid, private=@private', array(
      '@nid' => $node->nid,
      '@private' => $node->private,
    )));
  }
}