function ResponsiveImageStyleForm::save

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

Overrides EntityForm::save

File

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

Class

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

Namespace

Drupal\responsive_image

Code

public function save(array $form, FormStateInterface $form_state) {
    
    /** @var \Drupal\responsive_image\ResponsiveImageStyleInterface $responsive_image_style */
    $responsive_image_style = $this->entity;
    // Remove all the existing mappings and replace with submitted values.
    $responsive_image_style->removeImageStyleMappings();
    if ($form_state->hasValue('keyed_styles')) {
        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') {
                    $mapping = [
                        'image_mapping_type' => 'sizes',
                        'image_mapping' => [
                            'sizes' => $image_style_mapping['sizes'],
                            'sizes_image_styles' => array_keys(array_filter($image_style_mapping['sizes_image_styles'])),
                        ],
                    ];
                    $responsive_image_style->addImageStyleMapping($breakpoint_id, $multiplier, $mapping);
                }
                elseif ($image_style_mapping['image_mapping_type'] === 'image_style') {
                    $mapping = [
                        'image_mapping_type' => 'image_style',
                        'image_mapping' => $image_style_mapping['image_style'],
                    ];
                    $responsive_image_style->addImageStyleMapping($breakpoint_id, $multiplier, $mapping);
                }
            }
        }
    }
    $responsive_image_style->save();
    $this->logger('responsive_image')
        ->notice('Responsive image style @label saved.', [
        '@label' => $responsive_image_style->label(),
    ]);
    $this->messenger()
        ->addStatus($this->t('Responsive image style %label saved.', [
        '%label' => $responsive_image_style->label(),
    ]));
    // Redirect to edit form after creating a new responsive image style or
    // after selecting another breakpoint group.
    if (!$responsive_image_style->hasImageStyleMappings()) {
        $form_state->setRedirect('entity.responsive_image_style.edit_form', [
            'responsive_image_style' => $responsive_image_style->id(),
        ]);
    }
    else {
        $form_state->setRedirectUrl($this->entity
            ->toUrl('collection'));
    }
}

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