You are here

public function BootstrapSiteAlertAdmin::submitForm in Bootstrap Site Alert 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 FormInterface::submitForm

File

src/Form/BootstrapSiteAlertAdmin.php, line 301

Class

BootstrapSiteAlertAdmin
Class BootstrapSiteAlertAdmin.

Namespace

Drupal\bootstrap_site_alert\Form

Code

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

  // Clear out all our old states to prevent erroneous default values.
  // There is no wildcard to get these so this is the easiest way.
  $string = 'bootstrap_site_alert';
  $this->database
    ->delete('key_value')
    ->condition('name', $this->database
    ->escapeLike($string) . "%", 'LIKE')
    ->execute();

  // Save the values to the state.
  foreach ($form_state
    ->getValues() as $key => $value) {
    if ($key === 'bsa_container') {
      foreach ($value["bsa_fieldset"] as $bs_key => $bs_value) {
        foreach ($bs_value as $inner_key => $inner_value) {

          // Clean up our page paths so it works well with page matcher.
          // A little janky but it works.
          if ($inner_key === 'bootstrap_site_alert_only_paths') {
            $text = array_filter(explode("\r\n", $inner_value), 'trim');
            foreach ($text as $item_key => $item) {
              $text[$item_key] = ltrim($item, '/');
            }
            $inner_value = implode("\r\n", $text);
          }
          $this->state
            ->set($inner_key . $bs_key, $inner_value);
        }
      }
    }
  }

  // Set our BS version.
  $ver = $form_state
    ->getValue('bootstrap_site_alert_version', FALSE);
  $this->state
    ->set('bootstrap_site_alert_version', $ver);

  // Set the count so the form renders right on start.
  $this->state
    ->set('bootstrap_site_alert_count', isset($bs_key) ? $bs_key + 1 : 1);
  $this->ajaxPressed = FALSE;

  // Save a random key so that we can use it to track a 'dismiss' action for
  // this particular alert.
  $random = new Random();
  $this->state
    ->set('bootstrap_site_alert_key', $random
    ->string(16, TRUE));

  // Flushes the pages after save.
  Cache::invalidateTags([
    'rendered',
  ]);
}