You are here

public function SimplePopupBlocksEditForm::submitForm in Simple Popup Blocks 8

Same name and namespace in other branches
  1. 8.2 src/Form/SimplePopupBlocksEditForm.php \Drupal\simple_popup_blocks\Form\SimplePopupBlocksEditForm::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 SimplePopupBlocksAddForm::submitForm

File

src/Form/SimplePopupBlocksEditForm.php, line 182

Class

SimplePopupBlocksEditForm
Form to add a database entry, with all the interesting fields.

Namespace

Drupal\simple_popup_blocks\Form

Code

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

  // Get a value by key.
  $first = $form_state
    ->get('simple_popup_blocks_id');
  if ($form_state
    ->getValue('type') == 0) {
    $identifier = $form_state
      ->getValue('block_list');
  }
  else {
    $identifier = $form_state
      ->getValue('custom_css');
  }
  $visit_counts = serialize($form_state
    ->getValue('visit_counts'));
  $delay = $form_state
    ->getValue('delay');
  if (empty($delay) || $delay < 0) {
    $delay = 0;
  }
  $width = $form_state
    ->getValue('width');
  if (empty($width) || $width < 0) {
    $width = 400;
  }

  // Save the submitted entry.
  $entry = [
    'pid' => $first,
    'identifier' => $identifier,
    'type' => $form_state
      ->getValue('type'),
    'css_selector' => $form_state
      ->getValue('css_selector'),
    'layout' => $form_state
      ->getValue('layout'),
    'visit_counts' => $visit_counts,
    'overlay' => $form_state
      ->getValue('overlay'),
    'trigger_method' => $form_state
      ->getValue('trigger_method'),
    'trigger_selector' => $form_state
      ->getValue('trigger_selector'),
    'escape' => $form_state
      ->getValue('escape'),
    'delay' => $delay,
    'minimize' => $form_state
      ->getValue('minimize'),
    'close' => $form_state
      ->getValue('close'),
    'width' => $width,
    'status' => $form_state
      ->getValue('status'),
  ];
  $return = SimplePopupBlocksStorage::update($entry, $this->database, $this->messenger);
  if ($return) {
    $this->messenger
      ->addMessage($this
      ->t('Popup settings has been updated Successfully.'));
    $url = Url::fromRoute('simple_popup_blocks.manage');
    $form_state
      ->setRedirectUrl($url);
  }
  else {
    $this->messenger
      ->addError($this
      ->t('Error while creating.'), 'error');
  }
}