You are here

public function ResponsiveImageStyleForm::save in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/responsive_image/src/ResponsiveImageStyleForm.php \Drupal\responsive_image\ResponsiveImageStyleForm::save()

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

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

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