public static function Select2::processSelect in Select 2 8
Processes a select list form element.
This process callback is mandatory for select fields, since all user agents automatically preselect the first available option of single (non-multiple) select lists.
Parameters
array $element: The form element to process.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
array $complete_form: The complete form structure.
Return value
array The processed element.
Overrides Select::processSelect
See also
_form_validate()
File
- src/
Element/ Select2.php, line 174
Class
- Select2
- Provides an select2 form element.
Namespace
Drupal\select2\ElementCode
public static function processSelect(&$element, FormStateInterface $form_state, &$complete_form) {
// Fill the options, because in autocomplete we cleared them and for the
// validation the at least selected options are needed.
if ($element['#autocomplete']) {
$value_callable = isset($element['#autocomplete_options_callback']) ? $element['#autocomplete_options_callback'] : NULL;
if (!$value_callable || !is_callable($value_callable)) {
$value_callable = '\\Drupal\\select2\\Element\\Select2::getValidSelectedOptions';
}
$element['#options'] = call_user_func_array($value_callable, [
$element,
$form_state,
]);
}
// We need to disable form validation, because with autocreation the options
// could contain non existing references. We still have validation in the
// entity reference field.
if ($element['#autocreate'] && $element['#target_type']) {
unset($element['#needs_validation']);
// Add back auto_create values.
$values = is_array($element['#value']) ? $element['#value'] : [
$element['#value'],
];
foreach ($values as $key => $value) {
if (is_string($key) && substr($key, 0, 4) === "\$ID:") {
// Set option and remove ID from label.
$element['#options'][$key] = substr($value, 0, 4) === "\$ID:" ? substr($value, 4) : $value;
}
elseif (!$element['#multiple'] && substr($value, 0, 4) === "\$ID:") {
$element['#options'][$value] = substr($value, 4);
}
}
}
if (!$element['#multiple'] && !isset($element['#options'][$element['#empty_value']])) {
$empty_option = [
$element['#empty_value'] => '',
];
$element['#options'] = $empty_option + $element['#options'];
}
// Set the type from select2 to select to get proper form validation.
$element['#type'] = 'select';
return $element;
}