You are here

public static function WebformImageSelectElementImages::processWebformImageSelectElementImages in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_image_select/src/Element/WebformImageSelectElementImages.php \Drupal\webform_image_select\Element\WebformImageSelectElementImages::processWebformImageSelectElementImages()

Processes a webform element image select images element.

File

modules/webform_image_select/src/Element/WebformImageSelectElementImages.php, line 72

Class

WebformImageSelectElementImages
Provides a form element for managing webform element options.

Namespace

Drupal\webform_image_select\Element

Code

public static function processWebformImageSelectElementImages(&$element, FormStateInterface $form_state, &$complete_form) {
  $element['#tree'] = TRUE;

  /** @var \Drupal\webform_image_select\WebformImageSelectImagesStorageInterface $webform_images_storage */
  $webform_images_storage = \Drupal::entityTypeManager()
    ->getStorage('webform_image_select_images');
  $webform_images = $webform_images_storage
    ->getImages();
  $t_args = [
    ':href' => Url::fromRoute('entity.webform_image_select_images.collection')
      ->toString(),
  ];
  $class_name = 'js-' . $element['#id'] . '-webform-image-select-images';

  // Select images.
  $element['images'] = [
    '#type' => 'select',
    '#description' => t('Please select <a href=":href">predefined images</a> or enter custom image.', $t_args),
    '#options' => [
      static::CUSTOM_OPTION => t('Custom images…'),
    ] + $webform_images,
    '#attributes' => [
      'class' => [
        $class_name,
      ],
    ],
    '#error_no_message' => TRUE,
    '#access' => count($webform_images) ? TRUE : FALSE,
    '#default_value' => isset($element['#default_value']) && !is_array($element['#default_value']) ? $element['#default_value'] : '',
  ];

  // Custom images.
  $element['custom'] = [
    '#type' => 'webform_image_select_images',
    '#title' => $element['#title'],
    '#title_display' => 'invisible',
    '#states' => [
      'visible' => [
        "select.{$class_name}" => [
          'value' => '',
        ],
      ],
    ],
    '#error_no_message' => TRUE,
    '#default_value' => isset($element['#default_value']) && !is_string($element['#default_value']) ? $element['#default_value'] : [],
  ];
  $element['#element_validate'] = [
    [
      get_called_class(),
      'validateWebformImageSelectElementImages',
    ],
  ];
  if (!empty($element['#states'])) {
    WebformFormHelper::processStates($element, '#wrapper_attributes');
  }
  return $element;
}