You are here

public function AmswapConfigForm::submitForm in Admin Menu Swap 8

Same name and namespace in other branches
  1. 7.2 src/Form/AmswapConfigForm.php \Drupal\amswap\Form\AmswapConfigForm::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 ConfigFormBase::submitForm

1 call to AmswapConfigForm::submitForm()
AmswapConfigForm::deletePair in src/Form/AmswapConfigForm.php

File

src/Form/AmswapConfigForm.php, line 227

Class

AmswapConfigForm
Class AmswapConfigForm.

Namespace

Drupal\amswap\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  parent::submitForm($form, $form_state);
  $trigger = $form_state
    ->getTriggeringElement();
  $pairs = [];
  $pair_index = 0;
  $num_pairs = $form_state
    ->get('num_pairs');
  $num_pairs = $num_pairs ? $num_pairs : 1;
  for ($i = 0; $i < $num_pairs; $i++) {
    $role = $form_state
      ->getValue('pair-' . $i . '-role');
    $menu = $form_state
      ->getValue('pair-' . $i . '-menu');
    if ($role && $menu) {
      $pairs[$pair_index]['role'] = $role;
      $pairs[$pair_index]['menu'] = $menu;
      $pair_index++;
    }
    elseif (strpos($trigger['#id'], 'delete') === FALSE) {
      $msg = $this
        ->t('Pair @i was missing either a role or a menu value, so was not saved.', [
        '@i' => $i + 1,
      ]);
      $this
        ->messenger()
        ->addWarning($msg, FALSE);
    }
  }
  $this
    ->config('amswap.amswapconfig')
    ->set('role_menu_pairs', $pairs)
    ->save();
  $url = Url::fromRoute('system.performance_settings');
  $link = Link::fromTextAndUrl('Clear caches', $url);
  $msg = $link
    ->toString() . ' to see the changes.';
  $rendered_msg = Markup::create($msg);
  $this
    ->messenger()
    ->addStatus($rendered_msg, FALSE);

  // drupal_flush_all_caches();
}