You are here

public function ResponsiveImageStyleForm::save in Zircon Profile 8.0

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 251
Contains \Drupal\responsive_image\ResponsiveImageStyleForm.

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 = array(
            'image_mapping_type' => 'sizes',
            'image_mapping' => array(
              '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 = array(
            '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.', array(
    '@label' => $responsive_image_style
      ->label(),
  ));
  drupal_set_message($this
    ->t('Responsive image style %label saved.', array(
    '%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', array(
      'responsive_image_style' => $responsive_image_style
        ->id(),
    ));
  }
  else {
    $form_state
      ->setRedirectUrl($this->entity
      ->urlInfo('collection'));
  }
}