You are here

public function AbjsExperienceForm::saveExperience in A/B Test JS 8

Same name and namespace in other branches
  1. 2.0.x src/Form/AbjsExperienceForm.php \Drupal\abjs\Form\AbjsExperienceForm::saveExperience()

Save data.

Parameters

array $form: The form.

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

File

src/Form/AbjsExperienceForm.php, line 152

Class

AbjsExperienceForm
Class for build experience form.

Namespace

Drupal\abjs\Form

Code

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

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

    // This is a new experience, so use insert.
    $this->database
      ->insert('abjs_experience')
      ->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 experience"));
  }
  $form_state
    ->setRedirect('abjs.experience_admin');
}