You are here

function RealisticDummyContentTermReferenceField::GetTid in Realistic Dummy Content 7

Returns the term id for a term which is either existing or created on the fly.

Let's say an entity (node) contains a term reference to the taxonomy vocabulary "location", and in the realistic dummy content file structure, "Australia" is used for the location. If "Australia" exists as a "location", then this function will return its tid. If not, the term will be created, and then the tid will be returned.

Parameters

$name: The string for the taxonomy term.

Return value

The associated pre-existing or just-created tid.

Throws

Exception

1 call to RealisticDummyContentTermReferenceField::GetTid()
RealisticDummyContentTermReferenceField::ValueFromFile_ in api/includes/RealisticDummyContentTaxonomyTermReferenceField.inc
Given a RealisticDummyContentFileGroup object, get a structured property

File

api/includes/RealisticDummyContentTaxonomyTermReferenceField.inc, line 51
Define RealisticDummyContentTermReferenceField autoload class.

Class

RealisticDummyContentTermReferenceField

Code

function GetTid($name) {
  $vocabularies = taxonomy_get_vocabularies();
  $field_info = field_info_field($this
    ->GetName());
  $candidate_existing_terms = array();
  foreach ($field_info['settings']['allowed_values'] as $vocabulary) {
    $vocabulary_name = $vocabulary['vocabulary'];
    foreach ($vocabularies as $vocabulary) {
      if ($vocabulary->machine_name == $vocabulary_name) {
        $candidate_existing_terms = array_merge($candidate_existing_terms, taxonomy_get_tree($vocabulary->vid));
      }
    }
  }
  foreach ($candidate_existing_terms as $candidate_existing_term) {
    if ($candidate_existing_term->name == $name) {
      return $candidate_existing_term->tid;
    }
  }
  if (!isset($vocabulary->vid)) {
    throw new Exception('Expecting the taxonomy term reference to reference at least one vocabulary');
  }
  $term = new stdClass();
  $term->name = $name;
  $term->vid = $vocabulary->vid;
  taxonomy_term_save($term);
  if ($term->tid) {
    return $term->tid;
  }
  else {
    throw new Exception('tid could not be determined');
  }
}