You are here

function translation_helpers_invoke_translation_change in Translation helpers 7

Same name and namespace in other branches
  1. 6 translation_helpers.module \translation_helpers_invoke_translation_change()

Invoke hook_node_translation_change() to allow modules to respond to a change in source translation.

Follows logic in translation_remove_from_set().

Sample implementation: <code> function example_node_translation_change($node) { if (isset($node->translation_change)) { // If there is only one node remaining, track by nid rather than tnid. Otherwise, use // the new tnid. $new = $node->translation_change['new_tnid'] == 0 ? $node->translation_change['remaining_nid'] : $node->translation_change['new_tnid']; db_update('example') ->fields(array('id' => $new)) ->condition('id', $node->translation_change['old_tnid']) ->execute(); } } </code>

1 call to translation_helpers_invoke_translation_change()
translation_helpers_node_delete in ./translation_helpers.module
Implement hook_node_delete().

File

./translation_helpers.module, line 41
Provides methods for other modules to use with translated content.

Code

function translation_helpers_invoke_translation_change($node) {
  if (isset($node->tnid)) {
    if (db_query('SELECT COUNT(*) FROM {node} WHERE tnid = :tnid', array(
      ':tnid' => $node->tnid,
    ))
      ->fetchField() == 1) {

      // There is only one node left in the set.
      $node->translation_change = array(
        'old_tnid' => $node->tnid,
        'new_tnid' => 0,
        // Determine the remaining former member of the translation set.
        // May be needed e.g. to reassign existing data from the tnid to this nid.
        'remaining_nid' => db_query('SELECT nid FROM {node} WHERE tnid = :tnid', array(
          ':tnid' => $node->tnid,
        ))
          ->fetchField(),
      );

      // Allow other modules to respond to the removal of this translation set.
      module_invoke_all('node_translation_change', $node);
    }
    else {

      // 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) {
        $node->translation_change = array(
          'old_tnid' => $node->tnid,
          'new_tnid' => db_query('SELECT nid FROM {node} WHERE tnid = :tnid ORDER BY translate ASC, nid ASC', array(
            ':tnid' => $node->tnid,
          ))
            ->fetchField(),
        );

        // Allow other modules to respond to the changed source for this translation set.
        module_invoke_all('node_translation_change', $node);
      }
    }
  }
}