You are here

public function CsvImporter::getNestedTid in Hierarchical Taxonomy Import 8

1 call to CsvImporter::getNestedTid()
CsvImporter::importTaxonomies in src/Importer/CsvImporter.php
This method returns a taxonomy term's id by going into depth.

File

src/Importer/CsvImporter.php, line 27

Class

CsvImporter

Namespace

Drupal\hierarchical_taxonomy_importer\Importer

Code

public function getNestedTid($vid, $data, $parent = 0) {

  // Current Term Id
  $term = $data[0];

  //      kint(count($data));
  //      kint($this->counter);
  //      print implode(",", $data);
  if (count($data) == 1 && empty($data[1])) {
    $parent_new = $this->entity_manager
      ->getStorage('taxonomy_term')
      ->create([
      'name' => $data[0],
      'vid' => $vid,
    ]);
    $parent_new
      ->save();
    $this->counter++;
    kint($parent_new);

    //$parent = $this->getNestedTid($vid, $data, $parent_new->id(), $this->counter);
  }
  if (!empty($data[$this->counter]) && !empty($data[$this->counter + 1])) {
    $parent_term = $this->entity_manager
      ->getStorage('taxonomy_term')
      ->loadByProperties([
      'name' => $data[$this->counter + 1],
      'vid' => $vid,
    ]);
    $parent_new = $this->entity_manager
      ->getStorage('taxonomy_term')
      ->create([
      'name' => $term,
      'parent' => array_keys($parent_term)[0],
      'vid' => $vid,
    ]);
    $parent_new
      ->save();
    $parent = array_keys($parent_term)[0];
    $this->counter++;
    $parent = $this
      ->getNestedTid($vid, $data, $parent, $this->counter);
  }
  else {
    return $data[$count];
  }
}