function TaxonomyTestTrait::createTerm

Same name in this branch
  1. 8.9.x core/modules/taxonomy/tests/src/Traits/TaxonomyTestTrait.php \Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait::createTerm()
Same name in other branches
  1. 9 core/modules/taxonomy/tests/src/Traits/TaxonomyTestTrait.php \Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait::createTerm()
  2. 10 core/modules/taxonomy/tests/src/Traits/TaxonomyTestTrait.php \Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait::createTerm()
  3. 11.x 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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.