You are here

public function AbjsExperienceForm::buildForm 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::buildForm()

Building form.

Parameters

array $form: The form.

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

int $eid: The ID of the item to be deleted.

Overrides FormInterface::buildForm

File

src/Form/AbjsExperienceForm.php, line 75

Class

AbjsExperienceForm
Class for build experience form.

Namespace

Drupal\abjs\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $eid = NULL) {
  $form = [];
  $experience_name_default = "";
  $experience_script_default = "";
  if (!empty($eid)) {
    $experience_result = $this->database
      ->query('SELECT name, script FROM {abjs_experience} WHERE eid = :eid', [
      ':eid' => $eid,
    ]);
    $experience = $experience_result
      ->fetchObject();
    if (empty($experience)) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('The requested experience does not exist.'), 'error');
      return $form;
    }
    $experience_name_default = $experience->name;
    $experience_script_default = $experience->script;
    $form['eid'] = [
      '#type' => 'value',
      '#value' => $eid,
    ];
  }
  $form['name'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Experience Name'),
    '#default_value' => $experience_name_default,
    '#size' => 30,
    '#maxlength' => 50,
    '#required' => TRUE,
  ];
  $form['script'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Experience Script'),
    '#default_value' => $experience_script_default,
    '#description' => $this
      ->t('Any valid javascript to load in head. Leave empty for a Control. Read the documentation for more examples.'),
    '#rows' => 3,
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['save'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
    '#weight' => 5,
    '#submit' => [
      '::saveExperience',
    ],
    '#attributes' => [
      'class' => [
        "button button-action button--primary",
      ],
    ],
  ];
  $form['actions']['cancel'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Cancel'),
    '#weight' => 10,
    '#submit' => [
      '::cancelExperience',
    ],
    '#limit_validation_errors' => [],
  ];
  if (!empty($eid)) {
    $form['actions']['delete'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Delete'),
      '#weight' => 15,
      '#submit' => [
        '::deleteExperience',
      ],
    ];
  }

  // Add ace code editor for syntax highlighting on the script field.
  if ($this
    ->configFactory()
    ->get('abjs.settings')
    ->get('ace') == 1) {
    $form['#attached']['library'][] = 'abjs/ace-editor';
  }
  return $form;
}