You are here

public function TextExtractorFormSettings::submitForm in Search API attachments 8

Same name and namespace in other branches
  1. 9.0.x src/Form/TextExtractorFormSettings.php \Drupal\search_api_attachments\Form\TextExtractorFormSettings::submitForm()

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 ConfigFormBase::submitForm

File

src/Form/TextExtractorFormSettings.php, line 138

Class

TextExtractorFormSettings
Configuration form.

Namespace

Drupal\search_api_attachments\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $config = $this
    ->config(static::CONFIGNAME);

  // It is due to the ajax.
  $extractor_plugin_id = $form_state
    ->getValue('extraction_method');
  if ($extractor_plugin_id) {
    $configuration = $config
      ->get($extractor_plugin_id . '_configuration');
    $extractor_plugin = $this
      ->getTextExtractorPluginManager()
      ->createInstance($extractor_plugin_id, $configuration);
    $extractor_plugin
      ->submitConfigurationForm($form, $form_state);
  }
  $config = $this
    ->configFactory()
    ->getEditable(static::CONFIGNAME);

  // Set the extraction method variable.
  $config
    ->set('extraction_method', $extractor_plugin_id);

  // Set the redad text files directly option.
  $config
    ->set('read_text_files_directly', $form_state
    ->getValue('read_text_files_directly'));

  // Set the preserving cache option.
  $config
    ->set('preserve_cache', $form_state
    ->getValue('preserve_cache'));
  $config
    ->save();

  // Test the extraction.
  $file = $this
    ->getTestFile();
  $error = '';
  $extracted_data = NULL;
  try {
    $extracted_data = $extractor_plugin
      ->extract($file);
  } catch (\Exception $e) {
    $error = $e
      ->getMessage();
  }
  $file
    ->delete();
  if (empty($extracted_data)) {
    if (empty($error)) {
      $error = $this
        ->t('No error message was catched');
    }
    $data = [
      'message' => $this
        ->t("Unfortunately, the extraction doesn't seem to work with this configuration! (@error)", [
        '@error' => $error,
      ]),
      'type' => 'error',
    ];
  }
  else {
    $data = [
      'message' => $this
        ->t('Extracted data: %extracted_data', [
        '%extracted_data' => $extracted_data,
      ]),
      'type' => 'ok',
    ];
  }
  $storage = [
    'extracted_test_text' => $data,
  ];
  $form_state
    ->setStorage($storage);
  $form_state
    ->setRebuild();
}