You are here

public function AbjsConditionForm::saveCondition in A/B Test JS 2.0.x

Same name and namespace in other branches
  1. 8 src/Form/AbjsConditionForm.php \Drupal\abjs\Form\AbjsConditionForm::saveCondition()

Save data.

Parameters

array $form: The form.

\Drupal\Core\Form\FormStateInterface $form_state: The state of forms.

File

src/Form/AbjsConditionForm.php, line 152

Class

AbjsConditionForm
Class for build form condition.

Namespace

Drupal\abjs\Form

Code

public function saveCondition(array &$form, FormStateInterface $form_state) {
  $user = $this->account;
  if ($form_state
    ->hasValue('cid')) {

    // This is a modified condition, so use update.
    $this->database
      ->update('abjs_condition')
      ->fields([
      'name' => $form_state
        ->getValue('name'),
      'script' => $form_state
        ->getValue('script'),
      'changed' => $this->time
        ->getRequestTime(),
      'changed_by' => $user
        ->id(),
    ])
      ->condition('cid', $form_state
      ->getValue('cid'), '=')
      ->execute();
    $this
      ->messenger()
      ->addMessage($this
      ->t("Successfully updated condition"));
  }
  else {

    // This is a new condition, so use insert.
    $this->database
      ->insert('abjs_condition')
      ->fields([
      'name' => $form_state
        ->getValue('name'),
      'script' => $form_state
        ->getValue('script'),
      'created' => $this->time
        ->getRequestTime(),
      'created_by' => $user
        ->id(),
      'changed' => $this->time
        ->getRequestTime(),
      'changed_by' => $user
        ->id(),
    ])
      ->execute();
    $this
      ->messenger()
      ->addMessage($this
      ->t("Successfully saved new condition"));
  }
  $form_state
    ->setRedirect('abjs.condition_admin');
}