You are here

public function FillPdfSettingsForm::validateForm in FillPDF 8.4

Same name and namespace in other branches
  1. 5.0.x src/Form/FillPdfSettingsForm.php \Drupal\fillpdf\Form\FillPdfSettingsForm::validateForm()

Form validation 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 FormBase::validateForm

File

src/Form/FillPdfSettingsForm.php, line 334

Class

FillPdfSettingsForm
Configure FillPDF settings form.

Namespace

Drupal\fillpdf\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  switch ($values['backend']) {
    case 'fillpdf_service':

      // @todo: Add validation for FillPDF Service.
      // See https://www.drupal.org/project/fillpdf/issues/3040899.
      break;
    case 'local_service':

      // Set the form_state value to the Config object without saving.
      $config = $this
        ->config('fillpdf.settings')
        ->set('local_service_endpoint', $values['local_service_endpoint']);

      // Check for FillPDF LocalServer.
      $status = FillPdf::checkLocalServiceEndpoint($this->httpClient, $config);
      if ($status === FALSE) {
        $error_message = $this
          ->t('FillPDF LocalService is not properly installed. Was unable to contact %endpoint', [
          '%endpoint' => $values['local_service_endpoint'],
        ]);
        $form_state
          ->setErrorByName('local_service_endpoint', $error_message);
      }
      break;
    case 'pdftk':

      // Check for pdftk.
      $status = FillPdf::checkPdftkPath($values['pdftk_path']);
      if ($status === FALSE) {
        $error_message = $this
          ->t('The path you have entered for <em>pdftk</em> is invalid. Please enter a valid path.');
        $form_state
          ->setErrorByName('pdftk_path', $error_message);
      }
      break;
  }
  $template_scheme = $values['template_scheme'];
  $schemes_to_prepare = array_filter($values['allowed_schemes']) + [
    $template_scheme => $template_scheme,
  ];
  foreach ($schemes_to_prepare as $scheme) {
    $uri = FillPdf::buildFileUri($scheme, 'fillpdf');
    if (!$this->fileSystem
      ->prepareDirectory($uri, FileSystemInterface::CREATE_DIRECTORY + FileSystemInterface::MODIFY_PERMISSIONS)) {
      $error_message = $this
        ->t('Could not automatically create the subdirectory %directory. Please check permissions before trying again.', [
        '%directory' => $this->fileSystem
          ->realpath($uri),
      ]);
      $form_state
        ->setErrorByName('template_scheme', $error_message);
    }
  }
}