You are here

public function ImporterService::import in Hierarchical Taxonomy Import 8

Parameters

string $vid: Vocabulary ID to import terms.

mixed $data: Array structure with values to import as terms.

int $count: Counter for total number of records checking to stop the execution.

int $row: Current row number for CSV row.

int $pointer: Current Column Pointer.

mixed $parent: Parent TID for term being created.

mixed $tag: This is a flag used for checking if there is a record on next level, same level or on top level.

Return value

mixed

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/services/ImporterService.php, line 56

Class

ImporterService
Class ImporterService.

Namespace

Drupal\hierarchical_taxonomy_importer\services

Code

public function import($vid, $data, $count = 0, $row = 0, $pointer = 0, $parent = 0, $tag = 1) {

  // If all levels of a parents have been traversed and new parent has comeup for
  // import then reset the levels counter back to 0 for hierarhichal connect.
  if (!empty($data[$row][0])) {
    $pointer = 0;
    $parent = 0;
  }

  // If all rows have been traversed then exit.
  if ($count >= count($data)) {
    return;
  }

  // Returns the index of csv column has a value in a current record row.
  $pointer = $this
    ->getIndexOfNonNullValues($data[$row]);

  // If pointer is null then return null.
  if (is_null($pointer)) {
    return;
  }

  // Current Column value.
  $term_name = $data[$row][$pointer];

  // Searching for existing term.
  // It finds pointer for levels and breaks upon finding it.
  // If not empty columns value then asking it's index to the current pointer
  // and break.
  // Assigning current offset where a record resides.
  $parent_term_name = "";

  // Parent Row for the current record being read.
  $parent_row = $this
    ->getParentRow($data, $row, $pointer);

  // This checks for the parent row.
  if ($parent_row >= 0 && $pointer > 0) {
    $parent_term_name = $data[$parent_row][$pointer - 1];

    // This is the parent term name of currently processed taxonomy term.
    $csv_tree = $this
      ->getParentTree($data, $row, $pointer);

    // If parent_term is not empty then fetch the parent term's information.
    if (!empty($parent_term_name)) {

      // All terms with the same name as the parent in csv.
      $matching_parents = $this->entityTypeManager
        ->getStorage('taxonomy_term')
        ->loadByProperties([
        'vid' => $vid,
        'name' => $parent_term_name,
      ]);
      $parent_tree = [];

      // Get all parents with the same name as the supposed parent and construct a list of term names.
      foreach ($matching_parents as $mp) {
        $term_list = $this->entityTypeManager
          ->getStorage('taxonomy_term')
          ->loadAllParents($mp
          ->id());
        foreach ($term_list as $term) {
          $parent_tree[$mp
            ->id()][] = $term
            ->getName();
        }
      }
      foreach ($parent_tree as $parent_id => $children_list) {
        if ($parent_tree[$parent_id] == $csv_tree) {
          $parent = $parent_id;
        }
      }
    }
  }

  // This will be used when current term is being added or generated on same level.
  $original_parent = $parent;
  $parent = $this
    ->updateActualParent($vid, $term_name, $parent, $tag);

  // This works when system progress to the next level and finds values for that.
  if (!empty($data[$row + 1][$pointer + 1])) {
    return $this
      ->import($vid, $data, $count + 1, $row + 1, $pointer + 1, $parent, $this::DIFFERENT_PARENT);
  }

  // This works when system progress to the same level for current parent.
  if (!empty($data[$row + 1][$pointer])) {
    return $this
      ->import($vid, $data, $count + 1, $row + 1, $pointer, $original_parent, $this::SAME_PARENT);
  }

  // If none of the conditions matched above, then it traverses back by levelling backwards.
  return $this
    ->import($vid, $data, $count + 1, $row + 1, $pointer - 1, $parent, $this::PREVIOUS_PARENT);
}