You are here

public function HighContrastConfigurationForm::validateForm in High contrast 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/HighContrastConfigurationForm.php, line 156

Class

HighContrastConfigurationForm
Class HighContrastConfigurationForm.

Namespace

Drupal\high_contrast\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  if ($this->moduleHandler
    ->moduleExists('file')) {

    // Handle file uploads.
    $validators = [
      'file_validate_is_image' => [],
    ];

    // Check for a new uploaded logo.
    $file = file_save_upload('logo_upload', $validators, FALSE, 0);
    if (isset($file)) {

      // File upload was attempted.
      if ($file) {

        // Put the temporary file in form_values so we can save it on submit.
        $form_state
          ->setValue('logo_upload', $file);
      }
      else {

        // File upload failed.
        $form_state
          ->setErrorByName('logo_upload', $this
          ->t('The logo could not be uploaded.'));
      }
    }

    // When intending to use the default logo, unset the logo_path.
    if ($form_state
      ->getValue('default_logo')) {
      $form_state
        ->unsetValue('logo_path');
    }

    // If the user provided a path for a logo, make sure a file exists at that
    // path.
    if ($form_state
      ->getValue('logo_path')) {
      $path = $this
        ->validatePath($form_state
        ->getValue('logo_path'));
      if (!$path) {
        $form_state
          ->setErrorByName('logo_path', $this
          ->t('The custom logo path is invalid.'));
      }
    }
  }
}