function SettingsForm::validateForm

Same name in other branches
  1. 9 core/modules/aggregator/src/Form/SettingsForm.php \Drupal\aggregator\Form\SettingsForm::validateForm()
  2. 8.9.x core/modules/aggregator/src/Form/SettingsForm.php \Drupal\aggregator\Form\SettingsForm::validateForm()
  3. 10 core/modules/navigation/src/Form/SettingsForm.php \Drupal\navigation\Form\SettingsForm::validateForm()

Overrides ConfigFormBase::validateForm

File

core/modules/navigation/src/Form/SettingsForm.php, line 179

Class

SettingsForm
Configure Navigation settings for this site.

Namespace

Drupal\navigation\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) : void {
    // If the upload element is not empty, try to adjust the image dimensions
    // if needed.
    if ($form_state->getValue('logo_path')) {
        $path = $this->validateLogoPath($form_state->getValue('logo_path'));
        if (!$path) {
            $form_state->setErrorByName('logo_path', $this->t('The custom logo path is invalid.'));
        }
    }
    if ($form_state->getValue('logo_provider') !== NavigationRenderer::LOGO_PROVIDER_CUSTOM) {
        $form_state->setValue('logo_upload', '');
        $form_state->setValue('logo_path', '');
    }
    else {
        $file = _file_save_upload_from_form($form['logo']['custom']['logo_upload'], $form_state, 0);
        if ($file) {
            $logo_dimensions = $this->adjustLogoDimensions($file);
            if (!$logo_dimensions) {
                $config = $this->config('navigation.settings');
                $width = $config->get('logo.width');
                $height = $config->get('logo.height');
                $form_state->setErrorByName('logo_upload', $this->t('Image dimensions are bigger than the expected %widthx%height pixels and cannot be used as the navigation logo.', [
                    '%width' => $width,
                    '%height' => $height,
                ]));
            }
            // Put the temporary file in form_values so we can save it on submit.
            $form_state->setValue('logo_upload', $file);
            $form_state->setValue('logo_path', $file->getFileUri());
            $form_state->setValue('logo_dimensions', $logo_dimensions);
        }
        if (empty($form_state->getValue('logo_path'))) {
            $form_state->setErrorByName('logo_path', 'An image file is required with the current logo handling option.');
        }
    }
    parent::validateForm($form, $form_state);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.