You are here

protected function TermCreationTrait::createTerms in Hierarchical Term Formatter 8

Recursive function used to create terms in a tree.

2 calls to TermCreationTrait::createTerms()
HierarchicalTermFormatterErrorTest::setUp in tests/src/Functional/HierarchicalTermFormatterErrorTest.php
HierarchicalTermFormatterTest::setUp in tests/src/Functional/HierarchicalTermFormatterTest.php

File

tests/src/Functional/TermCreationTrait.php, line 13

Class

TermCreationTrait
Provides convenience method for creating terms.

Namespace

Drupal\Tests\hierarchical_term_formatter\Functional

Code

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());
    }
  }
}