You are here

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

Same name and namespace in other branches
  1. 8 src/Plugin/search_api_attachments/PythonPdf2txtExtractor.php \Drupal\search_api_attachments\Plugin\search_api_attachments\PythonPdf2txtExtractor::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/PythonPdf2txtExtractor.php, line 82

Class

PythonPdf2txtExtractor
Provides python pdf2text 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',
  ]);
  $python_path = $values['python_path'];
  $python_pdf2txt_script = $values['python_pdf2txt_script'];

  // Check that the file exists.
  if (!file_exists($python_pdf2txt_script)) {
    $form_state
      ->setError($form['text_extractor_config']['python_pdf2txt_script'], $this
      ->t('The file %path does not exist.', [
      '%path' => $python_pdf2txt_script,
    ]));
  }
  else {
    $cmd = escapeshellcmd($python_path) . ' ' . escapeshellarg($python_pdf2txt_script);
    exec($cmd, $output, $return_code);

    // $return_code = 1 if it fails. 100 instead.
    if ($return_code != 100) {
      $form_state
        ->setError($form['text_extractor_config']['python_path'], '');
      $form_state
        ->setError($form['text_extractor_config']['python_pdf2txt_script'], $this
        ->t('Python Pdf2txt script file is not executable.'));
    }
  }
}