You are here

public function PreExistingTermMultipleParentImportExportTest::createTerms in Acquia Content Hub 8.2

Creates a particular set of terms.

Parameters

array $keys: An array of keys.

2 calls to PreExistingTermMultipleParentImportExportTest::createTerms()
PreExistingTermMultipleParentImportExportTest::testTermImportExport1 in tests/src/Kernel/PreExistingTermMultipleParentImportExportTest.php
Performs tests with taxonomy term with multiple parents.
PreExistingTermMultipleParentImportExportTest::testTermImportExport2 in tests/src/Kernel/PreExistingTermMultipleParentImportExportTest.php
Performs tests with taxonomy term with multiple parents.

File

tests/src/Kernel/PreExistingTermMultipleParentImportExportTest.php, line 118

Class

PreExistingTermMultipleParentImportExportTest
Tests terms with multiple parents during export and import.

Namespace

Drupal\Tests\acquia_contenthub\Kernel

Code

public function createTerms(array $keys) {
  foreach ($keys as $key) {
    $values = $this->termValues[$key];
    $parents = $values['parent'];
    if (!empty($parents)) {
      $term_parents = [];
      foreach ($parents as $parent) {

        // Load parent term.
        $parent_name = $this->termValues[$parent]['name'];
        $term_parent = $this->entityTypeManager
          ->getStorage('taxonomy_term')
          ->loadByProperties([
          'name' => $parent_name,
        ]);
        $term_parent = reset($term_parent);
        if (!empty($term_parent)) {
          $term_parents[] = $term_parent
            ->id();
        }
      }
      $values['parent'] = $term_parents;
    }
    $term = $this->entityTypeManager
      ->getStorage('taxonomy_term')
      ->create($values);
    $term
      ->save();
    $this->terms[$key] = $term;
  }
}