You are here

public function AddUpdateForm::buildForm in Optimizely 8.0

Same name and namespace in other branches
  1. 8 src/AddUpdateForm.php \Drupal\optimizely\AddUpdateForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/AddUpdateForm.php, line 33
Contains \Drupal\optimizely\AddUpdateForm

Class

AddUpdateForm
Implements the form for the Add Projects page.

Namespace

Drupal\optimizely

Code

public function buildForm(array $form, FormStateInterface $form_state, $target_oid = NULL) {
  $addupdate_form = array();
  $addupdate_form['#theme'] = 'optimizely_add_update_form';
  $form['#attached']['library'][] = 'optimizely/optimizely.forms';
  if ($target_oid == NULL) {
    $form_action = 'Add';
    $intro_message = '';
    $addupdate_form['optimizely_oid'] = array(
      '#type' => 'value',
      '#value' => NULL,
    );

    // Enable form element defaults - blank, unselected
    $enabled = FALSE;
    $project_code = '';
  }
  else {
    $form_action = 'Update';
    $query = db_select('optimizely', 'o', array(
      'target' => 'slave',
    ))
      ->fields('o')
      ->condition('o.oid', $target_oid, '=');
    $record = $query
      ->execute()
      ->fetchObject();
    $addupdate_form['optimizely_oid'] = array(
      '#type' => 'value',
      '#value' => $target_oid,
    );
    $addupdate_form['optimizely_original_path'] = array(
      '#type' => 'value',
      '#value' => implode("\n", unserialize($record->path)),
    );
    $enabled = $record->enabled;
    $project_code = $record->project_code == 0 ? 'Undefined' : $record->project_code;
  }

  // If we are updating the default record, make the form element inaccessible
  $addupdate_form['optimizely_project_title'] = array(
    '#type' => 'textfield',
    '#disabled' => $target_oid == 1 ? TRUE : FALSE,
    '#title' => $this
      ->t('Project Title'),
    '#default_value' => $target_oid ? $record->project_title : '',
    '#description' => $target_oid == 1 ? $this
      ->t('Default project, this field can not be changed.') : $this
      ->t('Descriptive name for the project entry.'),
    '#size' => 60,
    '#maxlength' => 256,
    '#required' => TRUE,
    '#weight' => 10,
  );
  $account_id = AccountId::getId();
  $addupdate_form['optimizely_project_code'] = array(
    '#type' => 'textfield',
    '#disabled' => $target_oid == 1 ? TRUE : FALSE,
    '#title' => $this
      ->t('Optimizely Project Code'),
    '#default_value' => $project_code,
    '#description' => $account_id == 0 ? $this
      ->t('The Optimizely account value has not been set in the' . ' <a href="@url">Account Info</a> settings form.' . ' The Optimizely account value is used as' . ' the project ID for this "default" project entry.', array(
      '@url' => \Drupal::url('optimizely.settings'),
    )) : $this
      ->t('The Optimizely javascript file name used in the snippet' . ' as provided by the Optimizely website for the project.'),
    '#size' => 30,
    '#maxlength' => 100,
    '#required' => TRUE,
    '#weight' => 20,
  );
  $addupdate_form['optimizely_path'] = array(
    '#type' => 'textarea',
    '#title' => $this
      ->t('Set Path Where Optimizely Code Snippet Appears'),
    '#default_value' => $target_oid ? implode("\n", unserialize($record->path)) : '',
    '#description' => $this
      ->t('Enter the path where you want to insert the Optimizely' . ' Snippet. For Example: "/clubs/*" causes the snippet to appear on all pages' . ' below "/clubs" in the URL but not on the actual "/clubs" page itself.'),
    '#cols' => 100,
    '#rows' => 6,
    '#resizable' => FALSE,
    '#required' => FALSE,
    '#weight' => 40,
  );
  $addupdate_form['optimizely_enabled'] = array(
    '#type' => 'radios',
    '#title' => $this
      ->t('Enable/Disable Project'),
    '#default_value' => $target_oid ? $record->enabled : 0,
    '#options' => array(
      1 => 'Enable project',
      0 => 'Disable project',
    ),
    '#weight' => 25,
    '#attributes' => $enabled ? array(
      'class' => array(
        'enabled',
      ),
    ) : array(
      'class' => array(
        'disabled',
      ),
    ),
  );
  $addupdate_form['submit'] = array(
    '#type' => 'submit',
    '#value' => $form_action,
    '#weight' => 100,
  );
  $addupdate_form['cancel'] = array(
    '#markup' => \Drupal::l(t('Cancel'), new Url('optimizely.settings')),
    '#weight' => 101,
  );
  return $addupdate_form;
}