You are here

public function MappingEditFormBase::semiErImport in GatherContent 8.4

Same name and namespace in other branches
  1. 8.5 gathercontent_ui/src/Form/MappingEditFormBase.php \Drupal\gathercontent_ui\Form\MappingEditFormBase::semiErImport()

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 MappingEditFormBase::semiErImport()
MappingEditForm::submitForm in gathercontent_ui/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

gathercontent_ui/src/Form/MappingEditFormBase.php, line 380

Class

MappingEditFormBase
Class MappingEditFormBase.

Namespace

Drupal\gathercontent_ui\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++;
      }
    }
  }
}