You are here

function i18ntaxonomy_token_values in Internationalization 6

Implementation of hook_token_values().

File

i18ntaxonomy/i18ntaxonomy.module, line 966
i18n taxonomy module

Code

function i18ntaxonomy_token_values($type, $object = NULL, $options = array()) {
  $values = array();
  switch ($type) {
    case 'taxonomy':
      $term = $object;
      $values['i18n-term-raw'] = i18nstrings("taxonomy:term:{$term->tid}:name", $term->name);
      $values['i18n-term'] = check_plain(i18nstrings("taxonomy:term:{$term->tid}:name", $term->name));
      break;
    case 'node':
      $node = $object;

      // This code is copied from the token module which i adapting
      // pathauto's handling code; it's intended for compatability with it.
      if (!empty($node->taxonomy) && is_array($node->taxonomy)) {
        foreach ($node->taxonomy as $term) {
          $original_term = $term;
          if ((object) $term) {

            // With freetagging it's somewhat hard to get the tid, vid, name values
            // Rather than duplicating taxonomy.module code here you should make sure your calling module
            // has a weight of at least 1 which will run after taxonomy has saved the data which allows us to
            // pull it out of the db here.
            if (!isset($term->name) || !isset($term->tid)) {
              $vid = db_result(db_query_range("SELECT t.vid FROM {term_node} r INNER JOIN {term_data} t ON r.tid = t.tid INNER JOIN {vocabulary} v ON t.vid = v.vid WHERE r.nid = %d ORDER BY v.weight, t.weight, t.name", $object->nid, 0, 1));
              if (!$vid) {
                continue;
              }
              $term = db_fetch_object(db_query_range("SELECT t.tid, t.name FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.nid = %d ORDER BY weight", $vid, $object->nid, 0, 1));
              $term->vid = $vid;
            }

            // Ok, if we still don't have a term name maybe this is a pre-taxonomy submit node
            // So if it's a number we can get data from it
            if (!isset($term->name) && is_array($original_term)) {
              $tid = array_shift($original_term);
              if (is_numeric($tid)) {
                $term = taxonomy_get_term($tid);
              }
            }
            $vid = $term->vid;

            // If term names are localizable, we translate them to the node's
            // content language, not to the interface language' in which the
            // current user is viewing the site. (Creation of node tokens should
            // not depend on 'unpredictable' conditions like these.)
            // If node is language neutral, language is set to NULL.
            if (i18ntaxonomy_vocabulary($vid) == I18N_TAXONOMY_LOCALIZE && $node->language) {
              $values['i18n-term-raw'] = i18nstrings("taxonomy:term:{$term->tid}:name", $term->name, $node->language);
              $values['i18n-term'] = check_plain(i18nstrings("taxonomy:term:{$term->tid}:name", $term->name, $node->language));
            }
            else {
              $values['i18n-term-raw'] = $term->name;
              $values['i18n-term'] = check_plain($term->name);
            }
            break;
          }
        }
      }

      // It's possible to leave that block and still not have good data.
      // So, we test for these and if not set, set them.
      if (!isset($values['i18n-term'])) {
        $values['i18n-term-raw'] = '';
        $values['i18n-term'] = '';
      }
      break;
  }
  return $values;
}