You are here

class WebformImageSelectImages in Webform 6.x

Same name in this branch
  1. 6.x modules/webform_image_select/src/Element/WebformImageSelectImages.php \Drupal\webform_image_select\Element\WebformImageSelectImages
  2. 6.x modules/webform_image_select/src/Entity/WebformImageSelectImages.php \Drupal\webform_image_select\Entity\WebformImageSelectImages
Same name and namespace in other branches
  1. 8.5 modules/webform_image_select/src/Element/WebformImageSelectImages.php \Drupal\webform_image_select\Element\WebformImageSelectImages

Provides a form element to assist in creation of webform select image images.

Plugin annotation

@FormElement("webform_image_select_images");

Hierarchy

Expanded class hierarchy of WebformImageSelectImages

2 #type uses of WebformImageSelectImages
WebformImageSelectElementImages::processWebformImageSelectElementImages in modules/webform_image_select/src/Element/WebformImageSelectElementImages.php
Processes a webform element image select images element.
WebformImageSelectImagesForm::form in modules/webform_image_select/src/WebformImageSelectImagesForm.php
Gets the actual form array to be built.

File

modules/webform_image_select/src/Element/WebformImageSelectImages.php, line 16

Namespace

Drupal\webform_image_select\Element
View source
class WebformImageSelectImages extends FormElement {

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    return [
      '#input' => TRUE,
      '#label' => t('image'),
      '#labels' => t('images'),
      '#min_items' => 3,
      '#empty_items' => 1,
      '#add_more_items' => 1,
      '#process' => [
        [
          $class,
          'processWebformImageSelectImages',
        ],
      ],
      '#theme_wrappers' => [
        'form_element',
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
    if ($input === FALSE) {
      if (!isset($element['#default_value'])) {
        return [];
      }
      return $element['#default_value'];
    }
    elseif (is_array($input) && isset($input['images'])) {
      return $input['images'];
    }
    else {
      return NULL;
    }
  }

  /**
   * Process images and build images widget.
   */
  public static function processWebformImageSelectImages(&$element, FormStateInterface $form_state, &$complete_form) {
    $element['#tree'] = TRUE;

    // Add validate callback that extracts the associative array of images.
    $element['#element_validate'] = [
      [
        get_called_class(),
        'validateWebformImageSelectImages',
      ],
    ];

    // Wrap this $element in a <div> that handle #states.
    WebformElementHelper::fixStatesWrapper($element);
    $properties = [
      '#label',
      '#labels',
      '#min_items',
      '#empty_items',
      '#add_more_items',
    ];
    $element['images'] = array_intersect_key($element, array_combine($properties, $properties)) + [
      '#type' => 'webform_multiple',
      '#key' => 'value',
      '#header' => [
        [
          'data' => t('Image value'),
          'width' => '25%',
        ],
        [
          'data' => t('Image text'),
          'width' => '25%',
        ],
        [
          'data' => t('Image src'),
          'width' => '50%',
        ],
      ],
      '#element' => [
        'value' => [
          '#type' => 'textfield',
          '#title' => t('Image value'),
          '#title_display' => 'invisible',
          '#placeholder' => t('Enter value…'),
          '#error_no_message' => TRUE,
          '#attributes' => [
            'class' => [
              'js-webform-options-sync',
            ],
          ],
        ],
        'text' => [
          '#type' => 'textfield',
          '#title' => t('Image text'),
          '#title_display' => 'invisible',
          '#placeholder' => t('Enter text…'),
          '#error_no_message' => TRUE,
        ],
        'src' => [
          '#type' => 'textfield',
          '#title' => t('Image src'),
          '#title_display' => 'invisible',
          '#placeholder' => t('Enter image src…'),
          '#error_no_message' => TRUE,
        ],
      ],
      '#error_no_message' => TRUE,
      '#add_more_input_label' => t('more images'),
      '#default_value' => isset($element['#default_value']) ? $element['#default_value'] : [],
    ];
    if (function_exists('imce_process_url_element')) {
      $src_element =& $element['images']['#element']['src'];
      imce_process_url_element($src_element, 'link');
      $element['#attached']['library'][] = 'webform/imce.input';
    }
    elseif (\Drupal::currentUser()
      ->hasPermission('administer modules')) {
      $element['imce_message'] = [
        '#type' => 'webform_message',
        '#message_type' => 'warning',
        '#message_message' => t('Install the <a href=":href">IMCE module</a> to manage and uploaded image files.', [
          ':href' => 'https://www.drupal.org/project/imce',
        ]),
        '#message_close' => TRUE,
        '#message_id' => 'webform_imce_message',
        '#message_storage' => WebformMessage::STORAGE_LOCAL,
      ];
    }
    $element['#attached']['library'][] = 'webform/webform.element.options.admin';
    return $element;
  }

  /**
   * Validates webform image select images element.
   */
  public static function validateWebformImageSelectImages(&$element, FormStateInterface $form_state, &$complete_form) {
    $options = NestedArray::getValue($form_state
      ->getValues(), $element['images']['#parents']);

    // Validate required images.
    if (!empty($element['#required']) && empty($options)) {
      WebformElementHelper::setRequiredError($element, $form_state);
      return;
    }
    $form_state
      ->setValueForElement($element, $options);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
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. 27
MessengerTrait::messenger public function Gets the messenger. 27
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 2
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. 98
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
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.
WebformImageSelectImages::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
WebformImageSelectImages::processWebformImageSelectImages public static function Process images and build images widget.
WebformImageSelectImages::validateWebformImageSelectImages public static function Validates webform image select images element.
WebformImageSelectImages::valueCallback public static function Determines how user input is mapped to an element's #value property. Overrides FormElement::valueCallback