public function AddUpdateForm::buildForm in Optimizely 8
Same name and namespace in other branches
- 8.0 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 25
Class
- AddUpdateForm
- Implements the form for the Add Projects page.
Namespace
Drupal\optimizelyCode
public function buildForm(array $form, FormStateInterface $form_state, $target_oid = NULL) {
$addupdate_form = [];
$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'] = [
'#type' => 'value',
'#value' => NULL,
];
// Enable form element defaults - blank, unselected.
$enabled = FALSE;
$project_code = '';
}
else {
$form_action = 'Update';
$query = \Drupal::database()
->select('optimizely', 'o', [
'target' => 'slave',
])
->fields('o')
->condition('o.oid', $target_oid, '=');
$record = $query
->execute()
->fetchObject();
$addupdate_form['optimizely_oid'] = [
'#type' => 'value',
'#value' => $target_oid,
];
$addupdate_form['optimizely_original_path'] = [
'#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'] = [
'#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'] = [
'#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.', [
'@url' => Url::fromRoute('optimizely.settings')
->toString(),
]) : $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'] = [
'#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'] = [
'#type' => 'radios',
'#title' => $this
->t('Enable/Disable Project'),
'#default_value' => $target_oid ? $record->enabled : 0,
'#options' => [
1 => 'Enable project',
0 => 'Disable project',
],
'#weight' => 25,
'#attributes' => $enabled ? [
'class' => [
'enabled',
],
] : [
'class' => [
'disabled',
],
],
];
$addupdate_form['submit'] = [
'#type' => 'submit',
'#value' => $form_action,
'#weight' => 100,
];
$addupdate_form['cancel'] = [
'#markup' => Link::fromTextAndUrl(t('Cancel'), new Url('optimizely.settings'))
->toString(),
'#weight' => 101,
];
return $addupdate_form;
}