You are here

trait TermCreationTrait in Hierarchical Term Formatter 8

Provides convenience method for creating terms.

Hierarchy

File

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

Namespace

Drupal\Tests\hierarchical_term_formatter\Functional
View 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

Namesort descending Modifiers Type Description Overrides
TermCreationTrait::createTerms protected function Recursive function used to create terms in a tree.