You are here

public function AmswapConfigForm::submitForm in Admin Menu Swap 7.2

Same name and namespace in other branches
  1. 8 src/Form/AmswapConfigForm.php \Drupal\amswap\Form\AmswapConfigForm::submitForm()
1 call to AmswapConfigForm::submitForm()
AmswapConfigForm::amswap_delete_pair in src/Form/AmswapConfigForm.php

File

src/Form/AmswapConfigForm.php, line 138

Class

AmswapConfigForm
Class AmswapConfigForm.

Namespace

Drupal\amswap\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // kint($form, '$form');
  // kint($form_state, '$form_state');
  parent::submitForm($form, $form_state);
  $trigger = $form_state
    ->getTriggeringElement();

  // kint($trigger, '$trigger');
  $pairs = [];
  $pair_index = 0;
  $num_pairs = $form_state
    ->get('num_pairs');
  $num_pairs = $num_pairs ? $num_pairs : 1;

  // kint($num_pairs, '$num_pairs');
  for ($i = 0; $i < $num_pairs; $i++) {

    //   $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 = t('Pair ' . ($i + 1) . ' was missing either a role or a menu value, so was not saved.');
      drupal_set_message($msg, 'warning', FALSE);
    }
  }

  // kint($pairs, '$pairs');
  $this
    ->config('amswap.amswapconfig')
    ->set('role_menu_pairs', $pairs)
    ->save();
  $url = \Drupal\Core\Url::fromRoute('system.performance_settings');

  // kint($url, '$url');
  $link = \Drupal\Core\Link::fromTextAndUrl('Clear caches', $url);

  // kint($link, '$link');
  $msg = $link
    ->toString() . ' to see the changes.';
  $rendered_msg = \Drupal\Core\Render\Markup::create($msg);
  drupal_set_message($rendered_msg, 'status', FALSE);

  // drupal_flush_all_caches();
}