You are here

public function MappingEditFormBase::manualErImport 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::manualErImport()

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 MappingEditFormBase::manualErImport()
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 298

Class

MappingEditFormBase
Class MappingEditFormBase.

Namespace

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