You are here

function translation_remove_from_set in Drupal 7

Same name and namespace in other branches
  1. 6 modules/translation/translation.module \translation_remove_from_set()

Removes a node from its translation set and updates accordingly.

Parameters

$node: A node object.

1 call to translation_remove_from_set()
translation_node_delete in modules/translation/translation.module
Implements hook_node_delete().

File

modules/translation/translation.module, line 430
Manages content translations.

Code

function translation_remove_from_set($node) {
  if (isset($node->tnid) && $node->tnid) {
    $query = db_update('node')
      ->fields(array(
      'tnid' => 0,
      'translate' => 0,
    ));

    // Determine which nodes to apply the update to.
    $set_nids = db_query('SELECT nid FROM {node} WHERE tnid = :tnid', array(
      ':tnid' => $node->tnid,
    ))
      ->fetchCol();
    if (count($set_nids) == 1) {

      // There is only one node left in the set: remove the set altogether.
      $query
        ->condition('tnid', $node->tnid)
        ->execute();
      $flush_set = TRUE;
    }
    else {
      $query
        ->condition('nid', $node->nid)
        ->execute();

      // If the node being removed was the source of the translation set,
      // we pick a new source - preferably one that is up to date.
      if ($node->tnid == $node->nid) {
        $new_tnid = db_query('SELECT nid FROM {node} WHERE tnid = :tnid ORDER BY translate ASC, nid ASC', array(
          ':tnid' => $node->tnid,
        ))
          ->fetchField();
        db_update('node')
          ->fields(array(
          'tnid' => $new_tnid,
        ))
          ->condition('tnid', $node->tnid)
          ->execute();
        $flush_set = TRUE;
      }
    }

    // Flush the modified nodes from the load cache.
    $nids = !empty($flush_set) ? $set_nids : array(
      $node->nid,
    );
    entity_get_controller('node')
      ->resetCache($nids);
  }
}