You are here

public function MappingEditForm::semiErImport in GatherContent 8.3

Handle semiautomatic import of taxonomy terms.

Parameters

array|null $languages: Array with languages available for mapping.

Drupal\Core\Entity\EntityStorageInterface $entityStorage: Storage object for taxonomy terms.

array $row: Array with mapping options.

array $options: GatherContent options for every language and every field.

string $vid: Taxonomy vocabulry identifier.

1 call to MappingEditForm::semiErImport()
MappingEditForm::submitForm in src/Form/MappingEditForm.php
This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state…

File

src/Form/MappingEditForm.php, line 1228

Class

MappingEditForm
Class MappingEditForm.

Namespace

Drupal\gathercontent\Form

Code

public function semiErImport($languages, EntityStorageInterface $entityStorage, array $row, array $options, $vid) {
  if (!empty($languages)) {
    $terms = $entityStorage
      ->loadByProperties([
      'gathercontent_option_ids' => $row[$languages[0]],
    ]);

    /** @var \Drupal\taxonomy\Entity\Term $term */
    $term = array_shift($terms);
    if (!empty($term)) {
      foreach ($languages as $language) {
        if (!empty($row[$language]) && $term
          ->hasTranslation($language) && $term
          ->getTranslation($language)
          ->label() !== $options[$row[$language]]) {
          $term
            ->getTranslation($language)
            ->setName($options[$row[$language]]);
        }
      }
      $term
        ->save();
      $this->erImported++;
    }
    else {
      $term = Term::create([
        'vid' => $vid,
      ]);
      foreach ($languages as $language) {
        if (!empty($row[$language])) {
          if (!$term
            ->hasTranslation($language)) {
            $term
              ->addTranslation($language);
          }
          $term
            ->getTranslation($language)
            ->set('gathercontent_option_ids', $row[$language]);
          $term
            ->getTranslation($language)
            ->setName($options[$row[$language]]);
        }
      }
      if (!empty($term
        ->getTranslationLanguages())) {
        $term
          ->save();
        $this->erImported++;
      }
    }
  }
  else {

    /** @var \Drupal\taxonomy\Entity\Term $term */
    $und_lang_value = $row[LanguageInterface::LANGCODE_NOT_SPECIFIED];
    if (!empty($und_lang_value)) {
      $terms = $entityStorage
        ->loadByProperties([
        'gathercontent_option_ids' => $und_lang_value,
      ]);
      $term = array_shift($terms);
      if (!empty($term)) {
        if ($term
          ->label() !== $options[$und_lang_value]) {
          $term
            ->setName($options[$und_lang_value]);
        }
        $term
          ->save();
        $this->erImported++;
      }
      else {
        $term = Term::create([
          'vid' => $vid,
          'gathercontent_option_ids' => $und_lang_value,
        ]);
        $term
          ->setName($options[$und_lang_value]);
        $term
          ->save();
        $this->erImported++;
      }
    }
  }
}