public static function ShsTermSelect::processSelect in Webform Simple Hierarchical Select 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/ ShsTermSelect.php, line 40
Class
- ShsTermSelect
- Provides a webform element for an shs term select menu.
Namespace
Drupal\webform_shs\ElementCode
public static function processSelect(&$element, FormStateInterface $form_state, &$complete_form) {
self::setOptions($element);
$element = parent::processSelect($element, $form_state, $complete_form);
// Must convert this element['#type'] to a 'select' to prevent
// "Illegal choice %choice in %name element" validation error.
// @see \Drupal\Core\Form\FormValidator::performRequiredValidation
$element['#type'] = 'select';
// AJAX errors occur when submitting the element config on the webform ui
// path. \Drupal\shs\Plugin\Field\FieldWidget\OptionsShsWidget also stops
// rendering on the field ui pages.
$route = \Drupal::routeMatch()
->getRouteObject();
if (\Drupal::service('router.admin_context')
->isAdminRoute($route)) {
return $element;
}
$settings = [
'required' => $element['#required'],
'multiple' => $element['#webform_multiple'],
'anyLabel' => $element['#empty_option'] ?? t('- None -'),
'anyValue' => '_none',
'force_deepest' => $element['#force_deepest'],
'cache_options' => $element['#cache_options'],
'#depth_labels' => [],
'addNewLabel' => $element['#addNewLabel'] ?: t('Add another item'),
];
/** @var \Drupal\shs\WidgetDefaults $widget_defaults */
$widget_defaults = \Drupal::service('shs.widget_defaults');
$bundle = $element['#vocabulary'];
$cardinality = $element['#multiple'] ? -1 : 1;
// Define default parents for the widget.
$parents = $widget_defaults
->getInitialParentDefaults($settings['anyValue'], $cardinality);
if (!is_null($element['#value'])) {
$parents = $widget_defaults
->getParentDefaults($element['#value'], $settings['anyValue'], 'taxonomy_term', $cardinality);
}
$settings_shs = [
'settings' => $settings,
'labels' => $element['#depth_labels'],
'bundle' => $bundle,
'baseUrl' => 'shs-term-data',
'cardinality' => $cardinality,
'parents' => $parents,
'defaultValue' => $element['#value'] ?? NULL,
];
$hooks = [
'shs_js_settings',
sprintf('shs_%s_js_settings', $element['#webform_key']),
];
// Allow other modules to override the settings.
\Drupal::moduleHandler()
->alter($hooks, $settings_shs, $bundle, $element['#webform_key']);
$element['#shs'] = $settings_shs;
$element['#shs']['classes'] = shs_get_class_definitions($element['#webform_key']);
$element['#attributes']['class'][] = 'shs-enabled';
$element['#attributes']['data-shs-selector'] = $element['#webform_key'];
$element['#attached']['library'][] = 'shs/shs.form';
$element['#attached']['drupalSettings']['shs'] = [
$element['#webform_key'] => $element['#shs'],
];
$element['#element_validate'][] = [
self::class,
'validateForceDeepest',
];
return $element;
}