You are here

public function ThemeSettingsForm::validateForm in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Form/ThemeSettingsForm.php \Drupal\system\Form\ThemeSettingsForm::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

core/modules/system/src/Form/ThemeSettingsForm.php, line 356
Contains \Drupal\system\Form\ThemeSettingsForm.

Class

ThemeSettingsForm
Displays theme configuration for entire site and individual themes.

Namespace

Drupal\system\Form

Code

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

    // Handle file uploads.
    $validators = array(
      'file_validate_is_image' => array(),
    );

    // 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.'));
      }
    }
    $validators = array(
      'file_validate_extensions' => array(
        'ico png gif jpg jpeg apng svg',
      ),
    );

    // Check for a new uploaded favicon.
    $file = file_save_upload('favicon_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('favicon_upload', $file);
      }
      else {

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

    // If the user provided a path for a logo or favicon file, 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.'));
      }
    }
    if ($form_state
      ->getValue('favicon_path')) {
      $path = $this
        ->validatePath($form_state
        ->getValue('favicon_path'));
      if (!$path) {
        $form_state
          ->setErrorByName('favicon_path', $this
          ->t('The custom favicon path is invalid.'));
      }
    }
  }
}