ThemeSettingsForm::validateForm

public ThemeSettingsForm::validateForm(array &$form, FormStateInterface $form_state)

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 344

Class

ThemeSettingsForm
Displays theme configuration for entire site and individual themes.

Namespace

Drupal\system\Form

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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.'));
      }
    }
 
    // When intending to use the default logo, unset the logo_path.
    if ($form_state->getValue('default_logo')) {
      $form_state->unsetValue('logo_path');
    }
 
    // When intending to use the default favicon, unset the favicon_path.
    if ($form_state->getValue('default_favicon')) {
      $form_state->unsetValue('favicon_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.'));
      }
    }
    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.'));
      }
    }
  }
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.