You are here

public function TikaExtractor::validateConfigurationForm in Search API attachments 9.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/search_api_attachments/TikaExtractor.php \Drupal\search_api_attachments\Plugin\search_api_attachments\TikaExtractor::validateConfigurationForm()

Form validation handler.

Parameters

array $form: An associative array containing the structure of the plugin form as built by static::buildConfigurationForm().

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Overrides TextExtractorPluginBase::validateConfigurationForm

File

src/Plugin/search_api_attachments/TikaExtractor.php, line 88

Class

TikaExtractor
Provides tika extractor.

Namespace

Drupal\search_api_attachments\Plugin\search_api_attachments

Code

public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValue([
    'text_extractor_config',
  ]);
  $java_path = $values['java_path'];
  $tika_path = $values['tika_path'];

  // Check java path.
  exec($java_path, $output, $return_code);

  // $return_code = 127 if it fails. 1 instead.
  if ($return_code != 1) {
    $form_state
      ->setError($form['text_extractor_config']['java_path'], $this
      ->t('Invalid path or filename %path for java executable.', [
      '%path' => $java_path,
    ]));
    return;
  }

  // Check tika path.
  if (!file_exists($tika_path)) {
    $form_state
      ->setError($form['text_extractor_config']['tika_path'], $this
      ->t('Invalid path or filename %path for tika application jar.', [
      '%path' => $tika_path,
    ]));
  }
  else {
    $cmd = $java_path . ' -jar ' . escapeshellarg($tika_path) . ' -V';
    exec($cmd, $output, $return_code);

    // $return_code = 1 if it fails. 0 instead.
    if ($return_code) {
      $form_state
        ->setError($form['text_extractor_config']['tika_path'], $this
        ->t('Tika could not be reached and executed.'));
    }
    else {
      $this
        ->getMessenger()
        ->addStatus(t('Tika can be reached and be executed'));
    }
  }
}