You are here

public static function SampleDataLoader::getTerm in YAML Content 8.2

Helper function to get or create a term.

Parameters

string $term_name: The name of the term.

string $vocabulary: The term vocabulary.

Return value

int The id of the term.

1 call to SampleDataLoader::getTerm()
SampleDataLoader::loadSample in modules/sample_data/src/SampleDataLoader.php
Load sample data based on type and additional parameters.

File

modules/sample_data/src/SampleDataLoader.php, line 229

Class

SampleDataLoader
Provides methods for retrieving sample data to be used in demo content.

Namespace

Drupal\sample_data

Code

public static function getTerm($term_name, $vocabulary) {
  if ($terms = taxonomy_term_load_multiple_by_name($term_name, $vocabulary)) {
    $term = reset($terms);
  }
  else {
    $term = Term::create([
      'name' => $term_name,
      'vid' => $vocabulary,
    ]);
    $term
      ->save();
  }
  return $term
    ->id();
}