You are here

function nodeblock_node_update in Nodeblock 7

Implements hook_node_update().

File

./nodeblock.module, line 534
Enables use of specified node types as custom blocks.

Code

function nodeblock_node_update($node) {
  if (!nodeblock_type_enabled($node->type)) {
    nodeblock_node_delete($node);
    return;
  }

  // Check if this node already has a nodeblock entry.
  $nid = db_select('nodeblock', 'nb')
    ->condition('nid', $node->nid)
    ->fields('nb', array(
    'nid',
  ))
    ->execute()
    ->fetchColumn();
  if ($nid) {
    $values = _nodeblock_prepare_nodeblock($node);

    // Update machine name.
    $old_machine_name = db_select('nodeblock', 'nb')
      ->condition('nid', $node->nid)
      ->fields('nb', array(
      'machine_name',
    ))
      ->execute()
      ->fetchColumn();
    if ($old_machine_name && $old_machine_name != $values['machine_name']) {
      db_update('block')
        ->condition('module', 'nodeblock')
        ->condition('delta', $old_machine_name)
        ->fields(array(
        'delta' => $values['machine_name'],
      ))
        ->execute();
    }

    // Update nodeblock table.
    db_update('nodeblock')
      ->condition('nid', $node->nid)
      ->fields($values)
      ->execute();
    $node->nodeblock = $values;
  }
  else {
    nodeblock_node_insert($node);
  }
}