You are here

public function MimeDetectSettingsForm::validateForm in MimeDetect 8

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/MimeDetectSettingsForm.php, line 101

Class

MimeDetectSettingsForm
Configure MimeDetect settings for this site.

Namespace

Drupal\mimedetect\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $msg = '';

  // Test custom magic file path.
  $magic_file = $form_state
    ->getValue('magicfile');
  if (!empty($magic_file) && !MimeDetectService::checkMagicfile($form_state
    ->getValue('magicfile'), $msg)) {
    $form_state
      ->setErrorByName('magicfile', $msg);
    return;
  }

  // Test fileinfo settings.
  if ($form_state
    ->getValue('fileinfo_enable') && !MimeDetectService::checkFileinfo($magic_file, $msg)) {
    if (!empty($magic_file)) {
      $form_state
        ->setErrorByName('magicfile', $msg);
    }
    else {
      $form_state
        ->setErrorByName('fileinfo_enable', $msg);
    }
  }

  // Test file binary settings.
  if ($form_state
    ->getValue('unixfile_enable') && !MimeDetectService::checkUnixfile($form_state
    ->getValue('unixfile_binary'), $magic_file, $msg)) {
    if (!empty($magic_file)) {
      $form_state
        ->setErrorByName('magicfile', $msg);
    }
    else {
      $form_state
        ->setErrorByName('unixfile_binary', $msg);
    }
  }

  // Warning about no real MIME detection enable.
  if (!$form_state
    ->getValue('fileinfo_enable') && !$form_state
    ->getValue('unixfile_enable')) {
    $this
      ->messenger()
      ->addWarning($this
      ->t("MimeDetect is using the browser supplied filename for file extension lookups. It is strongly recommended that you install and configure the PHP Fileinfo Extension or the UNIX 'file' command to provide more accurate sever-side mime type detection."));
  }
  parent::validateForm($form, $form_state);
}