You are here

public function StyleswitcherStyleForm::submitForm in Style Switcher 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Form/StyleswitcherStyleForm.php \Drupal\styleswitcher\Form\StyleswitcherStyleForm::submitForm()

Form submission handler.

Parameters

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

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

Overrides FormInterface::submitForm

File

src/Form/StyleswitcherStyleForm.php, line 152

Class

StyleswitcherStyleForm
Provides a form to add/edit a style.

Namespace

Drupal\styleswitcher\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $old_name = $form_state
    ->getValue('old_name');
  $styles = styleswitcher_custom_styles();
  $style = [
    'label' => $form_state
      ->getValue('label'),
    'name' => 'custom/' . $form_state
      ->getValue('name'),
    'path' => $form_state
      ->getValue('path'),
  ];
  if ($old_name !== '') {
    unset($styles[$old_name]);

    // Update style keys in settings variable.
    if ($style['name'] != $old_name) {
      $config = $this
        ->configFactory()
        ->getEditable('styleswitcher.styles_settings');
      $settings = $config
        ->get('settings') ?? [];
      foreach (array_keys($settings) as $theme) {
        if (isset($settings[$theme][$old_name])) {
          $settings[$theme][$style['name']] = $settings[$theme][$old_name];
          unset($settings[$theme][$old_name]);
        }
      }
      $config
        ->set('settings', $settings)
        ->save();
    }
  }
  $styles[$style['name']] = $style;
  $this
    ->configFactory()
    ->getEditable('styleswitcher.custom_styles')
    ->set('styles', $styles)
    ->save();
  $this
    ->messenger()
    ->addStatus($this
    ->t('The style %title has been saved.', [
    '%title' => $style['label'],
  ]));
  $form_state
    ->setRedirect('styleswitcher.admin');
}