public static function WebformImageSelect::processSelect in Webform 6.x
Same name and namespace in other branches
- 8.5 modules/webform_image_select/src/Element/WebformImageSelect.php \Drupal\webform_image_select\Element\WebformImageSelect::processSelect()
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
- modules/
webform_image_select/ src/ Element/ WebformImageSelect.php, line 38
Class
- WebformImageSelect
- Provides a webform element for a selecting an image.
Namespace
Drupal\webform_image_select\ElementCode
public static function processSelect(&$element, FormStateInterface $form_state, &$complete_form) {
// Convert #images to #options.
static::setOptions($element);
// Set show label.
if ($element['#show_label']) {
$element['#attributes']['data-show-label'] = 'data-show-label';
}
// Add label filter.
if ($element['#show_label'] && $element['#filter']) {
$field_prefix = isset($element['#field_prefix']) ? $element['#field_prefix'] : NULL;
$wrapper_class = 'js-' . Html::getClass($element['#name'] . '-filter');
$element['#wrapper_attributes']['class'][] = $wrapper_class;
$singular = !empty($element['#filter__singular']) ? $element['#filter__singular'] : t('image');
$plural = !empty($element['#filter__plural']) ? $element['#filter__plural'] : t('images');
$count = count($element['#images']);
$element['#field_prefix'] = [
'filter' => [
'#type' => 'search',
'#id' => $element['#id'] . '-filter',
'#name' => $element['#name'] . '_filter',
'#title' => t('Filter'),
'#title_display' => 'invisible',
'#size' => 30,
'#placeholder' => !empty($element['#filter__placeholder']) ? $element['#filter__placeholder'] : t('Filter images by label'),
'#attributes' => [
'class' => [
'webform-form-filter-text',
],
'data-focus' => 'false',
'data-item-singlular' => $singular,
'data-item-plural' => $plural,
'data-summary' => ".{$wrapper_class} .webform-image-select-summary",
'data-no-results' => ".{$wrapper_class} .webform-image-select-no-results",
'data-element' => ".{$wrapper_class} .thumbnails",
'data-source' => ".thumbnail p",
'data-parent' => 'li',
'data-selected' => '.selected',
'title' => t('Enter a keyword to filter by.'),
],
'#wrapper_attributes' => [
'class' => [
'webform-image-select-filter',
],
],
'#field_suffix' => [
'info' => [
'#type' => 'html_tag',
'#tag' => 'span',
'#attributes' => [
'class' => [
'webform-image-select-summary',
],
],
'content' => [
'#markup' => t('@count @items', [
'@count' => $count,
'@items' => $count === 1 ? $singular : $plural,
]),
],
],
'no_results' => [
'#type' => 'webform_message',
'#attributes' => [
'style' => 'display:none',
'class' => [
'webform-image-select-no-results',
],
],
'#message_message' => !empty($element['#filter__no_results']) ? $element['#filter__no_results'] : t('No images found.'),
'#message_type' => 'info',
],
],
],
];
if ($field_prefix) {
$element['#field_prefix']['field_prefix'] = is_array($element['#field_prefix']) ? $element['#field_prefix'] : [
'#markup' => $element['#field_prefix'],
];
}
}
// Serialize images as JSON to 'data-images' attributes.
$element['#attributes']['data-images'] = Json::encode($element['#images']);
// Add classes.
$element['#attributes']['class'][] = 'webform-image-select';
$element['#attributes']['class'][] = 'js-webform-image-select';
// Attach library.
$element['#attached']['library'][] = 'webform_image_select/webform_image_select.element';
if ($element['#show_label'] && $element['#filter']) {
$element['#attached']['library'][] = 'webform/webform.filter';
}
return parent::processSelect($element, $form_state, $complete_form);
}