You are here

public function RealisticDummyContentTermReferenceField::getTid in Realistic Dummy Content 3.x

Same name and namespace in other branches
  1. 8.2 api/src/includes/RealisticDummyContentTermReferenceField.php \Drupal\realistic_dummy_content_api\includes\RealisticDummyContentTermReferenceField::getTid()
  2. 7.2 api/src/includes/RealisticDummyContentTermReferenceField.php \Drupal\realistic_dummy_content_api\includes\RealisticDummyContentTermReferenceField::getTid()

Returns 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.

If two terms have the same name, the tid of the first will be returned.


string $name: The string for the taxonomy term.

Return value

int The associated pre-existing or just-created tid of the first term with the desired name.



1 call to RealisticDummyContentTermReferenceField::getTid()
RealisticDummyContentTermReferenceField::implementValueFromFile in api/src/includes/RealisticDummyContentTermReferenceField.php
Given a RealisticDummyContentFileGroup object, get a structured property.


api/src/includes/RealisticDummyContentTermReferenceField.php, line 54


Represents a term reference field.




public function getTid($name) {
  $vocabularies = $this
  $field_info = $this
  $candidate_existing_terms = [];
  foreach ($field_info['settings']['allowed_values'] as $setting) {
    $vocabulary_name = $setting['vocabulary'];
    foreach ($vocabularies as $vocabulary) {
      if ($this
        ->vocabularyMachineName($vocabulary) == $vocabulary_name) {
        $candidate_existing_terms = array_merge($candidate_existing_terms, $this
        break 2;
  foreach ($candidate_existing_terms as $candidate_existing_term) {
    $candidate_name = $this
    if ($candidate_name == $name) {
      return $this
  if (!isset($vocabulary)) {
    throw new \Exception('Expecting the taxonomy term reference to reference at least one vocabulary');
  $term = $this
    ->newVocabularyTerm($vocabulary, $name);
  return $this