trait TermCreationTrait in Hierarchical Term Formatter 8
Provides convenience method for creating terms.
Hierarchy
- trait \Drupal\Tests\hierarchical_term_formatter\Functional\TermCreationTrait
File
- tests/
src/ Functional/ TermCreationTrait.php, line 8
Namespace
Drupal\Tests\hierarchical_term_formatter\FunctionalView source
trait TermCreationTrait {
/**
* Recursive function used to create terms in a tree.
*/
protected function createTerms(array $items, $parent_id = 0) {
foreach ($items as $key => $item) {
$name = is_array($item) ? $key : $item;
$term = $this->container
->get('entity_type.manager')
->getStorage('taxonomy_term')
->create([
'name' => $name,
'vid' => 'numbers',
]);
if ($parent_id) {
$term
->set('parent', $parent_id);
}
$term
->save();
$this->createdTerms[$term
->label()] = $term
->id();
if (is_array($item)) {
$this
->createTerms($item, $term
->id());
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
TermCreationTrait:: |
protected | function | Recursive function used to create terms in a tree. |