You are here

public function ColorsSettingsForm::submitForm in Colors 8

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 ConfigFormBase::submitForm

File

src/Form/ColorsSettingsForm.php, line 107

Class

ColorsSettingsForm
Configure color settings.

Namespace

Drupal\colors\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $plugin = $form_state
    ->getValue('plugin');
  if ($plugin) {
    foreach ($form_state
      ->getValue('palette') as $id => $palette) {
      \Drupal::configFactory()
        ->getEditable($id)
        ->set('type', $plugin['id'])
        ->set('label', $plugin['title'])
        ->set('enabled', $form_state
        ->getValue($form_state
        ->getValue('id')))
        ->set('palette', $palette)
        ->set('weight', $plugin['weight'])
        ->save();
    }

    // Update settings.
    $config = \Drupal::configFactory()
      ->getEditable('colors.settings');
    $order = $config
      ->get('order');
    if ($form_state
      ->getValue($form_state
      ->getValue('id'))) {

      // Enabled.
      $order[$plugin['id']] = 0;
    }
    else {

      // Disabled.
      unset($order[$plugin['id']]);
    }
    $config
      ->set('order', $order)
      ->save();
  }
  else {
    $process = $form_state
      ->getValue('process_order');
    \Drupal::configFactory()
      ->getEditable('colors.settings')
      ->set('override', $process['enabled'])
      ->set('order', $process['enabled'] ? colors_get_weights($form_state) : [])
      ->set('palette', $form_state
      ->getValue('palette'))
      ->save();
  }
}