You are here

public function MappingEditForm::manualErImport in GatherContent 8.3

Handle manual type 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.

1 call to MappingEditForm::manualErImport()
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 1146

Class

MappingEditForm
Class MappingEditForm.

Namespace

Drupal\gathercontent\Form

Code

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

    /** @var \Drupal\taxonomy\Entity\Term $term */
    $term = array_shift($terms);

    // If term already exists.
    if (!empty($term)) {

      // If term was changed, remove option ids for every
      // language.
      if ($term
        ->id() !== $row['terms']) {

        // We don't know how many languages are translated.
        $translation_languages = $term
          ->getTranslationLanguages(TRUE);
        foreach ($translation_languages as $language) {
          if ($term
            ->hasTranslation($language) && !empty($row[$language])) {
            $option_ids = $term
              ->getTranslation($language)
              ->get('gathercontent_option_ids');
            foreach ($option_ids as $i => $option_id) {
              if ($option_id == $row[$language]) {
                unset($option_ids[$i]);
              }
            }
            $term
              ->getTranslation($language)
              ->set('gathercontent_option_ids', $option_ids);
          }
        }
      }
    }

    // Set new values to correct term.
    $term = Term::load($row['terms']);
    if (!empty($languages)) {
      foreach ($languages as $language) {
        $term
          ->getTranslation($language)
          ->set('gathercontent_option_ids', $row[$language]);
      }
    }
    $term
      ->save();
    $this->erImported++;
  }
  elseif (empty($languages) && !empty($row['terms'])) {
    $und_lang_value = $row[LanguageInterface::LANGCODE_NOT_SPECIFIED];
    if (!empty($und_lang_value)) {
      $terms = $entityStorage
        ->loadByProperties([
        'gathercontent_option_ids' => $und_lang_value,
      ]);

      /** @var \Drupal\taxonomy\Entity\Term $term */
      $term = array_shift($terms);

      // If term already exists.
      if (!empty($term)) {

        // If term was changed, remove option ids for every
        // language.
        if ($term
          ->id() !== $row['terms']) {
          $option_ids = $term
            ->get('gathercontent_option_ids');
          foreach ($option_ids as $i => $option_id) {
            if ($option_id == $und_lang_value) {
              unset($option_ids[$i]);
            }
          }
          $term
            ->set('gathercontent_option_ids', $option_ids);
        }
      }

      // Set new values to correct term.
      $term = Term::load($row['terms']);
      $term
        ->set('gathercontent_option_ids', $und_lang_value);
      $term
        ->save();
      $this->erImported++;
    }
  }
}