You are here

function _migrate_taxonomy_get_term in Migrate 6

Return a tid for a term (text).

Parameters

$vocab: Vocabulary object

$text: Name of term searched for

$handler: 'add' to automatically add the term to the vocabulary, 'warn' to simply test for existence

Return value

The tid of the existing or added term, or -1 if $handler is 'warn' and the term does not exist.

1 call to _migrate_taxonomy_get_term()
taxonomy_migrate_prepare_node in modules/taxonomy.migrate.inc
Implementation of hook_migrate_prepare_node().

File

modules/taxonomy.migrate.inc, line 317
Implementation of taxonomy destination handling

Code

function _migrate_taxonomy_get_term($vocab, $text, $handler) {
  static $missing_terms = array();
  $vid = $vocab->vid;
  if (!isset($missing_terms[$vid])) {
    $missing_terms[$vid] = array();
  }

  // Bail out for empty text.
  if (empty($text)) {
    return -1;
  }

  // If we have found this $text already, return it.
  if (isset($missing_terms[$vid][$text])) {
    return $missing_terms[$vid][$text];
  }

  // Try to find a term with a matching name.
  $possibilities = taxonomy_get_term_by_name($text);
  foreach ($possibilities as $possibility) {
    if ($possibility->vid == $vid) {
      $missing_terms[$vid][$text] = $possibility->tid;
      return $possibility->tid;
    }
  }

  // Try to find a term with a matching tid.
  if (is_numeric($text) && ($term = taxonomy_get_term($text))) {
    $missing_terms[$vid][$text] = $term->tid;
    return $term->tid;
  }

  // If we arrive here, the term does not exist.
  switch ($handler) {
    case 'add':
      $edit = array(
        'vid' => $vid,
        'name' => $text,
      );
      $status = taxonomy_save_term($edit);
      $tid = $edit['tid'];
      drupal_set_message(t('Added %term term to the %name vocabulary.', array(
        '%term' => theme('placeholder', $text),
        '%name' => theme('placeholder', $vocab->name),
      )));
      break;
    case 'warn':
      drupal_set_message(t('There is no %term term inside the %name vocabulary.', array(
        '%term' => theme('placeholder', $text),
        '%name' => theme('placeholder', $vocab->name),
      )));

    //Fall-through
    default:

      // which includes 'ignore' and 'no-import'
      $tid = -1;
      break;
  }
  $missing_terms[$vid][$text] = $tid;
  return $tid;
}