You are here

public function TaxonomyTestTrait::createTerm in Drupal 8

Same name in this branch
  1. 8 core/modules/taxonomy/src/Tests/TaxonomyTestTrait.php \Drupal\taxonomy\Tests\TaxonomyTestTrait::createTerm()
  2. 8 core/modules/taxonomy/tests/src/Traits/TaxonomyTestTrait.php \Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait::createTerm()

Returns a new term with random properties in vocabulary $vid.

Parameters

\Drupal\taxonomy\Entity\Vocabulary $vocabulary: The vocabulary object.

array $values: (optional) An array of values to set, keyed by property name. If the entity type has bundles, the bundle key has to be specified.

Return value

\Drupal\taxonomy\Entity\Term The new taxonomy term object.

File

core/modules/taxonomy/src/Tests/TaxonomyTestTrait.php, line 47

Class

TaxonomyTestTrait
Provides common helper methods for Taxonomy module tests.

Namespace

Drupal\taxonomy\Tests

Code

public function createTerm(Vocabulary $vocabulary, $values = []) {
  $filter_formats = filter_formats();
  $format = array_pop($filter_formats);
  $term = Term::create($values + [
    'name' => $this
      ->randomMachineName(),
    'description' => [
      'value' => $this
        ->randomMachineName(),
      // Use the first available text format.
      'format' => $format
        ->id(),
    ],
    'vid' => $vocabulary
      ->id(),
    'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $term
    ->save();
  return $term;
}