public static function WebformOptions::processWebformOptions in Webform 8.5
Same name and namespace in other branches
- 6.x src/Element/WebformOptions.php \Drupal\webform\Element\WebformOptions::processWebformOptions()
Process options and build options widget.
File
- src/
Element/ WebformOptions.php, line 74
Class
- WebformOptions
- Provides a webform element to assist in creation of options.
Namespace
Drupal\webform\ElementCode
public static function processWebformOptions(&$element, FormStateInterface $form_state, &$complete_form) {
$element['#tree'] = TRUE;
// Add validate callback that extracts the associative array of options.
$element += [
'#element_validate' => [],
];
array_unshift($element['#element_validate'], [
get_called_class(),
'validateWebformOptions',
]);
// Wrap this $element in a <div> that handle #states.
WebformElementHelper::fixStatesWrapper($element);
// For options with optgroup display a CodeMirror YAML editor.
if (!empty($element['#yaml']) || isset($element['#default_value']) && is_array($element['#default_value']) && static::hasOptGroup($element['#default_value'])) {
// Build table.
$element['options'] = [
'#type' => 'webform_codemirror',
'#mode' => 'yaml',
'#default_value' => WebformYaml::encode($element['#default_value']),
'#placeholder' => t('Enter custom options…'),
'#description' => t('Key-value pairs MUST be specified as "safe_key: \'Some readable options\'". Use of only alphanumeric characters and underscores is recommended in keys. One option per line.') . '<br /><br />' . t('Option groups can be created by using just the group name followed by indented group options.'),
];
return $element;
}
else {
$t_args = [
'@label' => isset($element['#label']) ? Unicode::ucfirst($element['#label']) : t('Options'),
];
$properties = [
'#label',
'#labels',
'#min_items',
'#empty_items',
'#add_more_items',
];
$element['options'] = array_intersect_key($element, array_combine($properties, $properties)) + [
'#type' => 'webform_multiple',
'#header' => TRUE,
'#key' => 'value',
'#default_value' => isset($element['#default_value']) ? static::convertOptionsToValues($element['#default_value'], $element['#options_description']) : [],
'#add_more_input_label' => t('more @options', [
'@options' => $element['#labels'],
]),
];
if ($element['#options_description']) {
$element['options']['#element'] = [
'option_value' => [
'#type' => 'container',
'#title' => t('@label value', $t_args),
'#help' => t('A unique value stored in the database.'),
'value' => [
'#type' => 'textfield',
'#title' => t('@label value', $t_args),
'#title_display' => 'invisible',
'#placeholder' => t('Enter value…'),
'#attributes' => [
'class' => [
'js-webform-options-sync',
],
],
'#maxlength' => $element['#options_value_maxlength'],
'#error_no_message' => TRUE,
],
],
'option_text' => [
'#type' => 'container',
'#title' => t('@label text / description', $t_args),
'#help' => t('Enter text and description to be displayed on the form.'),
'text' => [
'#type' => 'textfield',
'#title' => t('@label text', $t_args),
'#title_display' => 'invisible',
'#placeholder' => t('Enter text…'),
'#maxlength' => $element['#options_text_maxlength'],
'#error_no_message' => TRUE,
],
'description' => [
'#type' => 'textarea',
'#title' => t('@label description', $t_args),
'#title_display' => 'invisible',
'#placeholder' => t('Enter description…'),
'#rows' => 2,
'#maxlength' => $element['#options_description_maxlength'],
'#error_no_message' => TRUE,
],
],
];
}
else {
$element['options']['#element'] = [
'option_value' => [
'#type' => 'container',
'#title' => t('@label value', $t_args),
'#help' => t('A unique value stored in the database.'),
'value' => [
'#type' => 'textfield',
'#title' => t('@label value', $t_args),
'#title_display' => 'invisible',
'#placeholder' => t('Enter value…'),
'#attributes' => [
'class' => [
'js-webform-options-sync',
],
],
'#maxlength' => $element['#options_value_maxlength'],
'#error_no_message' => TRUE,
],
],
'option_text' => [
'#type' => 'container',
'#title' => t('@label text', $t_args),
'#help' => t('Text to be displayed on the form.'),
'text' => [
'#type' => 'textfield',
'#title' => t('@label text', $t_args),
'#title_display' => 'invisible',
'#placeholder' => t('Enter text…'),
'#maxlength' => $element['#options_text_maxlength'],
'#error_no_message' => TRUE,
],
],
];
}
$element['#attached']['library'][] = 'webform/webform.element.options.admin';
return $element;
}
}