You are here

public function SiteVerifyAdminForm::validateForm in Site verification 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/SiteVerifyAdminForm.php, line 147

Class

SiteVerifyAdminForm
Configure cron settings for this site.

Namespace

Drupal\site_verify\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $storage = $form_state
    ->getStorage();
  $values =& $form_state
    ->getValues();

  // Check META tag.
  $form_state
    ->setValue('meta', trim($values['meta']));
  if ($values['meta'] != '' && !preg_match('/<meta (.*)>/', $values['meta'])) {
    $form_state
      ->setErrorByName('meta', $this
      ->t('Only META tags are supported at this moment'));
  }

  // Check verification file.
  if ($storage['record']['engine']['file']) {

    // Import the uploaded verification file.
    $validators = [
      'file_validate_extensions' => [],
    ];
    if ($file = file_save_upload('file_upload', $validators, FALSE, 0, FileSystemInterface::EXISTS_REPLACE)) {
      $contents = @file_get_contents($file
        ->getFileUri());
      $file
        ->delete();
      if ($contents === FALSE) {
        $this
          ->messenger()
          ->addError(t('The verification file import failed, because the file %filename could not be read.', [
          '%filename' => $file
            ->getFilename(),
        ]));
      }
      else {
        $values['file'] = $file
          ->getFilename();
        $values['file_contents'] = $contents;
      }
    }
    if ($values['file']) {
      $existing_file = \Drupal::database()
        ->query("SELECT svid FROM {site_verify} WHERE LOWER(file) = LOWER(:file)", [
        ':file' => $values['file'],
      ])
        ->fetchField();
      if ($existing_file && $values['svid'] !== $existing_file) {
        $form_state
          ->setErrorByName('file', $this
          ->t('The file %filename is already being used in another verification.', [
          '%filename' => $values['file'],
        ]));
      }
    }
  }
}