function ResponsiveImageStyleForm::validateForm

Same name in other branches
  1. 9 core/modules/responsive_image/src/ResponsiveImageStyleForm.php \Drupal\responsive_image\ResponsiveImageStyleForm::validateForm()
  2. 10 core/modules/responsive_image/src/ResponsiveImageStyleForm.php \Drupal\responsive_image\ResponsiveImageStyleForm::validateForm()
  3. 11.x core/modules/responsive_image/src/ResponsiveImageStyleForm.php \Drupal\responsive_image\ResponsiveImageStyleForm::validateForm()

Overrides FormBase::validateForm

File

core/modules/responsive_image/src/ResponsiveImageStyleForm.php, line 220

Class

ResponsiveImageStyleForm
Form controller for the responsive image edit/add forms.

Namespace

Drupal\responsive_image

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
    // Only validate on edit.
    if ($form_state->hasValue('keyed_styles')) {
        // Check if another breakpoint group is selected.
        if ($form_state->getValue('breakpoint_group') != $form_state->getCompleteForm()['breakpoint_group']['#default_value']) {
            // Remove the image style mappings since the breakpoint ID has changed.
            $form_state->unsetValue('keyed_styles');
        }
        // Check that at least 1 image style has been selected when using sizes.
        foreach ($form_state->getValue('keyed_styles') as $breakpoint_id => $multipliers) {
            foreach ($multipliers as $multiplier => $image_style_mapping) {
                if ($image_style_mapping['image_mapping_type'] === 'sizes') {
                    if (empty($image_style_mapping['sizes'])) {
                        $form_state->setError($form['keyed_styles'][$breakpoint_id][$multiplier]['sizes'], 'Provide a value for the sizes attribute.');
                    }
                    if (empty(array_keys(array_filter($image_style_mapping['sizes_image_styles'])))) {
                        $form_state->setError($form['keyed_styles'][$breakpoint_id][$multiplier]['sizes_image_styles'], 'Select at least one image style.');
                    }
                }
            }
        }
    }
}

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