class WebformImageSelect in Webform 8.5
Same name in this branch
- 8.5 modules/webform_image_select/src/Element/WebformImageSelect.php \Drupal\webform_image_select\Element\WebformImageSelect
- 8.5 modules/webform_image_select/src/Plugin/WebformElement/WebformImageSelect.php \Drupal\webform_image_select\Plugin\WebformElement\WebformImageSelect
Same name and namespace in other branches
- 6.x modules/webform_image_select/src/Element/WebformImageSelect.php \Drupal\webform_image_select\Element\WebformImageSelect
Provides a webform element for a selecting an image.
Plugin annotation
@FormElement("webform_image_select");
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\Core\Render\Element\Select
- class \Drupal\webform_image_select\Element\WebformImageSelect
- class \Drupal\Core\Render\Element\Select
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of WebformImageSelect
1 file declares its use of WebformImageSelect
- WebformImageSelect.php in modules/
webform_image_select/ src/ Plugin/ WebformElement/ WebformImageSelect.php
1 #type use of WebformImageSelect
- WebformExampleCustomFormSettingsForm::buildForm in modules/
webform_example_custom_form/ src/ Form/ WebformExampleCustomFormSettingsForm.php - Form constructor.
File
- modules/
webform_image_select/ src/ Element/ WebformImageSelect.php, line 17
Namespace
Drupal\webform_image_select\ElementView source
class WebformImageSelect extends Select {
/**
* {@inheritdoc}
*/
public function getInfo() {
$info = parent::getInfo();
$info['#images'] = [];
$info['#images_randomize'] = FALSE;
$info['#show_label'] = FALSE;
$info['#filter'] = FALSE;
$info['#filter__placeholder'] = NULL;
$info['#filter__singular'] = NULL;
$info['#filter__plural'] = NULL;
$info['#filter__no_result'] = NULL;
return $info;
}
/**
* {@inheritdoc}
*/
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);
}
/**
* Set element #options from #images.
*
* @param array $element
* A Webform image select element.
*/
public static function setOptions(array &$element) {
// Randomize images.
if (!empty($element['#images_randomize'])) {
$element['#images'] = WebformElementHelper::randomize($element['#images']);
}
// Convert #images to #options and make sure images are keyed by value.
if (empty($element['#options'])) {
$options = [];
foreach ($element['#images'] as $value => &$image) {
if (isset($image['text'])) {
// Apply XSS filter to image text.
$image['text'] = WebformHtmlEditor::stripTags($image['text']);
// Strip all HTML tags from the option.
$options[$value] = strip_tags($image['text']);
}
else {
$options[$value] = $value;
}
}
$element['#options'] = $options;
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormElement:: |
public static | function | Adds autocomplete functionality to elements. | |
FormElement:: |
public static | function | #process callback for #pattern form element property. | |
FormElement:: |
public static | function | #element_validate callback for #pattern form element property. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
RenderElement:: |
public static | function | Adds Ajax information about an element to communicate with JavaScript. | |
RenderElement:: |
public static | function | Adds members of this group as actual elements for rendering. | |
RenderElement:: |
public static | function | Form element processing handler for the #ajax form property. | 1 |
RenderElement:: |
public static | function | Arranges elements into groups. | |
RenderElement:: |
public static | function |
Sets a form element's class attribute. Overrides ElementInterface:: |
|
Select:: |
public static | function | Prepares a select render element. | |
Select:: |
public static | function |
Determines how user input is mapped to an element's #value property. Overrides FormElement:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
WebformImageSelect:: |
public | function |
Returns the element properties for this element. Overrides Select:: |
|
WebformImageSelect:: |
public static | function |
Processes a select list form element. Overrides Select:: |
|
WebformImageSelect:: |
public static | function | Set element #options from #images. |