You are here

function translation_nodeapi_nat in Internationalization 5.2

Same name and namespace in other branches
  1. 5.3 translation/translation.module \translation_nodeapi_nat()
  2. 5 translation/translation.module \translation_nodeapi_nat()

NAT (Node As Term) integration for node translations

This will run after i18n, translation and nat have done their job

1 call to translation_nodeapi_nat()
translation_nodeapi in translation/translation.module
Implementation of hook_nodeapi().

File

translation/translation.module, line 312

Code

function translation_nodeapi_nat(&$node, $op) {
  $nat_config = variable_get('nat_config', array());

  // TO-DO: nat_get_terms has caching, look into it
  if (($op == 'insert' || $op == 'update') && isset($nat_config['types'][$node->type]) && !empty($nat_config['types'][$node->type]) && ($nat_terms = translation_nat_get_terms($node->nid))) {

    //drupal_set_message("DEBUG:translation_nodeapi_nat op=$op trid=$node->trid");
    $translations = $term_trids = array();

    // First of all, just update language for this node's terms.
    foreach ($nat_terms as $term) {
      db_query("UPDATE {term_data} SET language = '%s' WHERE tid = %d", $node->language, $term->tid);
      $translations[$term->vid][$node->language] = $term;
      if ($term->trid) {
        $term_trids[$term->vid] = $term->trid;
      }
    }

    // If the node is not in a translation set, there's nothing else to do
    if ($node->trid) {

      // Get node translations
      $node_translations = $node->trid ? translation_node_get_translations(array(
        'nid' => $node->nid,
      )) : array();

      // Now we build an array of the form $translations[vid][language] = $term,
      foreach ($node_translations as $trnode) {
        foreach (nat_get_terms($trnode->nid) as $term) {

          // If the term doesn't have a language we set that of the node it belongs to.
          // This should work for previously created terms.
          if (!$term->language) {
            $term->language = $trnode->language;
            db_query("UPDATE {term_data} SET language = '%s' WHERE tid = %d", $term->language, $term->tid);
          }
          $translations[$term->vid][$term->language] = $term;
          if ($term->trid) {

            // This will have some problem in the cases where terms for different nodes are in different
            // translation sets. That shouldn't happen though; so we force all of them in the same set.
            $term_trids[$term->vid] = $term->trid;
          }
        }
      }

      // Update the terms, assigning translations in the same vocabulary if possible
      foreach ($nat_terms as $term) {
        if (count($translations[$term->vid]) > 1) {
          $translation_set = $translations[$term->vid];
          $trid = isset($term_trids[$term->vid]) ? $term_trids[$term->vid] : 0;
          translation_taxonomy_save($translation_set, $trid);

          //drupal_set_message("DEBUG:translation_nodeapi_nat, trid=$trid data=".print_r($translation_set, TRUE));
        }
      }
    }
  }
}