You are here

public function SynonymImportForm::submitForm in Search API Synonym 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/SynonymImportForm.php, line 210

Class

SynonymImportForm
Class SynonymImportForm.

Namespace

Drupal\search_api_synonym\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  try {

    // All values from the form.
    $values = $form_state
      ->getValues();

    // Instance of active import plugin.
    $plugin_id = $values['plugin'];
    $instance = $this
      ->getPluginInstance($plugin_id);

    // Parse file.
    $data = $instance
      ->parseFile($values['file_upload'], (array) $values['plugin_settings'][$plugin_id]);

    // Import data.
    $importer = new Importer();
    $results = $importer
      ->execute($data, $values);
    if (!empty($results['errors'])) {
      $count = count($results['errors']);
      $message = \Drupal::translation()
        ->formatPlural($count, '@count synonym failed import.', '@count synonyms failed import.', [
        '@count' => $count,
      ]);
      $this
        ->messenger()
        ->addStatus($message);
    }
  } catch (ImportException $e) {
    $this
      ->logger('search_api_synonym')
      ->error($this
      ->t('Failed to import file due to "%error".', [
      '%error' => $e
        ->getMessage(),
    ]));
    $this
      ->messenger()
      ->addStatus($this
      ->t('Failed to import file due to "%error".', [
      '%error' => $e
        ->getMessage(),
    ]));
  }
}