public function YamlFormResultsCustomForm::buildForm in YAML Form 8
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/
Form/ YamlFormResultsCustomForm.php, line 78
Class
- YamlFormResultsCustomForm
- Form for form results custom(ize) form.
Namespace
Drupal\yamlform\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
$available_columns = $this->submissionStorage
->getColumns($this->yamlform, $this->sourceEntity, NULL, TRUE);
$custom_columns = $this->submissionStorage
->getCustomColumns($this->yamlform, $this->sourceEntity, NULL, TRUE);
// Change sid's # to an actual label.
$available_columns['sid']['title'] = $this
->t('Submission ID');
if (isset($custom_columns['sid'])) {
$custom_columns['sid']['title'] = $this
->t('Submission ID');
}
// Columns.
$columns_options = [];
foreach ($available_columns as $column_name => $column) {
$title = strpos($column_name, 'element__') === 0 ? [
'data' => [
'#markup' => '<b>' . $column['title'] . '</b>',
],
] : $column['title'];
$key = isset($column['key']) ? str_replace('yamlform_', '', $column['key']) : $column['name'];
$columns_options[$column_name] = [
'title' => $title,
'key' => $key,
];
}
$columns_keys = array_keys($custom_columns);
$columns_default_value = array_combine($columns_keys, $columns_keys);
$form['columns'] = [
'#type' => 'yamlform_tableselect_sort',
'#header' => [
'title' => $this
->t('Title'),
'key' => $this
->t('Key'),
],
'#options' => $columns_options,
'#default_value' => $columns_default_value,
];
// Get available sort options.
$sort_options = [];
$sort_columns = $available_columns;
ksort($sort_columns);
foreach ($sort_columns as $column_name => $column) {
if (!isset($column['sort']) || $column['sort'] === TRUE) {
$sort_options[$column_name] = (string) $column['title'];
}
}
asort($sort_options);
// Sort and direction.
// Display available columns sorted alphabetically.
$sort = $this->yamlform
->getState($this
->getStateKey('sort'), 'serial');
$direction = $this->yamlform
->getState($this
->getStateKey('direction'), 'desc');
$form['sort'] = [
'#prefix' => '<div class="container-inline">',
'#type' => 'select',
'#field_prefix' => $this
->t('Sort by'),
'#options' => $sort_options,
'#default_value' => $sort,
];
$form['direction'] = [
'#type' => 'select',
'#field_prefix' => ' ' . $this
->t('in') . ' ',
'#field_suffix' => ' ' . $this
->t('order.'),
'#options' => [
'asc' => $this
->t('Ascending (ASC)'),
'desc' => $this
->t('Descending (DESC)'),
],
'#default_value' => $direction,
'#suffix' => '</div>',
];
// Limit.
$limit = $this->yamlform
->getState($this
->getStateKey('limit'), NULL);
$form['limit'] = [
'#type' => 'select',
'#field_prefix' => $this
->t('Show'),
'#field_suffix' => $this
->t('results per page.'),
'#options' => [
'20' => '20',
'50' => '50',
'100' => '100',
'200' => '200',
'500' => '500',
'1000' => '1000',
'0' => $this
->t('All'),
],
'#default_value' => $limit != NULL ? $limit : 50,
];
// Default configuration.
if (empty($this->sourceEntity)) {
$form['config'] = [
'#type' => 'details',
'#title' => $this
->t('Configuration settings'),
];
$form['config']['default'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use as default configuration'),
'#description' => $this
->t('If checked, the above settings will be used as the default configuration for all associated YAML Form nodes.'),
'#return_value' => TRUE,
'#default_value' => $this->yamlform
->getState($this
->getStateKey('default'), TRUE),
];
}
// Format settings.
$format = $this->yamlform
->getState($this
->getStateKey('format'), [
'header_format' => 'label',
'element_format' => 'value',
]);
$form['format'] = [
'#type' => 'details',
'#title' => $this
->t('Format settings'),
'#tree' => TRUE,
];
$form['format']['header_format'] = [
'#type' => 'radios',
'#title' => $this
->t('Column header format'),
'#description' => $this
->t('Choose whether to show the element label or element key in each column header.'),
'#options' => [
'label' => $this
->t('Element titles (label)'),
'key' => $this
->t('Element keys (key)'),
],
'#default_value' => $format['header_format'],
];
$form['format']['element_format'] = [
'#type' => 'radios',
'#title' => $this
->t('Element format'),
'#options' => [
'value' => $this
->t('Labels/values, the human-readable value (value)'),
'raw' => $this
->t('Raw values, the raw value stored in the database (raw)'),
],
'#default_value' => $format['element_format'],
];
// Build actions.
$form['actions']['#type'] = 'actions';
$form['actions']['save'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
];
$form['actions']['delete'] = [
'#type' => 'submit',
'#value' => $this
->t('Reset'),
'#attributes' => [
'class' => [
'button',
'button--danger',
],
],
'#access' => $this->yamlform
->hasState($this
->getStateKey('columns')),
'#submit' => [
'::delete',
],
];
return $form;
}