You are here

class WebformImageSelect in Webform 8.5

Same name in this branch
  1. 8.5 modules/webform_image_select/src/Element/WebformImageSelect.php \Drupal\webform_image_select\Element\WebformImageSelect
  2. 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
  1. 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

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\Element
View 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

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormElement::processAutocomplete public static function Adds autocomplete functionality to elements.
FormElement::processPattern public static function #process callback for #pattern form element property.
FormElement::validatePattern public static function #element_validate callback for #pattern form element property.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
Select::preRenderSelect public static function Prepares a select render element.
Select::valueCallback public static function Determines how user input is mapped to an element's #value property. Overrides FormElement::valueCallback
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WebformImageSelect::getInfo public function Returns the element properties for this element. Overrides Select::getInfo
WebformImageSelect::processSelect public static function Processes a select list form element. Overrides Select::processSelect
WebformImageSelect::setOptions public static function Set element #options from #images.