You are here

public function GinLoginConfigurationForm::validateForm in Gin Login 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/GinLoginConfigurationForm.php, line 111

Class

GinLoginConfigurationForm
Class SettingsForm.

Namespace

Drupal\gin_login\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  $moduleHandler = \Drupal::service('module_handler');
  if ($moduleHandler
    ->moduleExists('file')) {

    // Check for a new uploaded logo.
    if (isset($form['logo'])) {
      $file = _file_save_upload_from_form($form['logo']['settings']['logo_upload'], $form_state, 0);
      if ($file) {

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

    // 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 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.'));
      }
    }

    // Check for a new uploaded Brand Image.
    if (isset($form['brand_image'])) {
      $file = _file_save_upload_from_form($form['brand_image']['settings']['brand_image_upload'], $form_state, 0);
      if ($file) {

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

    // When intending to use the default brand image, unset the brand_image_path.
    if ($form_state
      ->getValue('default_brand_image')) {
      $form_state
        ->unsetValue('brand_image_path');
    }

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