You are here

public static function WebformOptionsCustom::processWebformOptionsCustom in Webform 8.5

Same name and namespace in other branches
  1. 6.x modules/webform_options_custom/src/Element/WebformOptionsCustom.php \Drupal\webform_options_custom\Element\WebformOptionsCustom::processWebformOptionsCustom()

Processes an 'other' element.

See select list webform element for select list properties.

See also

\Drupal\Core\Render\Element\Select

File

modules/webform_options_custom/src/Element/WebformOptionsCustom.php, line 105

Class

WebformOptionsCustom
Provides an element for a selecting custom options from HTML or SVG markup.

Namespace

Drupal\webform_options_custom\Element

Code

public static function processWebformOptionsCustom(&$element, FormStateInterface $form_state, &$complete_form) {

  // Load config entity and set the element's #options and #template.
  static::setTemplateOptions($element);

  // Sanitize option descriptions which may have been altered.
  // Note: Option text is escaped via JavaScript.
  // @see webform_options_custom.element.js#initializeTemplateTooltip
  $descriptions = [];
  foreach ($element['#options'] as $option_value => $option_text) {
    if (WebformOptionsHelper::hasOptionDescription($option_text)) {
      list($option_text, $option_description) = WebformOptionsHelper::splitOption($option_text);
      $element['#options'][$option_value] = $option_text;
      $descriptions[$option_value] = Xss::filterAdmin($option_description);
    }
  }

  // Get inline template context.
  $template_context = WebformArrayHelper::removePrefix($element) + [
    'descriptions' => $descriptions,
  ];
  $element['#tree'] = TRUE;

  // Select menu.
  $element['select'] = [
    '#type' => 'select',
    '#options' => $element['#options'],
    '#title' => $element['#title'],
    '#webform_element' => TRUE,
    '#title_display' => 'invisible',
    '#error_no_message' => TRUE,
  ];
  $properties = static::$properties;
  $element['select'] += array_intersect_key($element, array_combine($properties, $properties));

  // Apply #parents to select element.
  if (isset($element['#parents'])) {
    $element['select']['#parents'] = array_merge($element['#parents'], [
      'select',
    ]);
  }

  // Initialize the select to allow for webform enhancements.

  /** @var \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager */
  $element_manager = \Drupal::service('plugin.manager.webform.element');
  $element_manager
    ->buildElement($element['select'], $complete_form, $form_state);
  if (preg_match('/(\\{\\{|\\{%)/', $element['#template'])) {

    // Build template using Twig when Twig syntax is found.
    $element['template'] = [
      '#type' => 'inline_template',
      '#template' => $element['#template'],
      '#context' => $template_context,
      '#prefix' => '<div class="webform-options-custom-template">',
      '#suffic' => '</div>',
    ];
  }
  else {

    // Build template using markup.
    $element['template'] = [
      '#markup' => Markup::create($element['#template']),
      '#prefix' => '<div class="webform-options-custom-template">',
      '#suffic' => '</div>',
    ];
  }

  // Set classes.
  $element['#attributes']['class'][] = 'js-webform-options-custom';
  $element['#attributes']['class'][] = 'webform-options-custom';
  if (!empty($element['#options_custom'])) {
    $webform_options_custom_class = Html::getClass($element['#options_custom']);
    $element['#attributes']['class'][] = 'js-webform-options-custom--' . $webform_options_custom_class;
    $element['#attributes']['class'][] = 'webform-options-custom--' . $webform_options_custom_class;
  }

  // Apply the element id to the wrapper so that inline form errors point
  // to the correct element.
  $element['#attributes']['id'] = $element['#id'];

  // Set SVG fill, zoom, tooltip, and show/hide select.
  if ($element['#fill']) {
    $element['#attributes']['data-fill'] = TRUE;
  }
  if ($element['#zoom']) {
    $element['#attributes']['data-zoom'] = TRUE;
  }
  if ($element['#tooltip']) {
    $element['#attributes']['data-tooltip'] = TRUE;
  }
  if (empty($element['#show_select'])) {
    $element['#attributes']['data-select-hidden'] = TRUE;
  }

  // Set descriptions.
  if ($descriptions) {
    $element['#attributes']['data-descriptions'] = Json::encode($descriptions);
  }

  // Remove options to prevent option validation on the element.
  unset($element['#options']);

  // Add validate callback.
  $element += [
    '#element_validate' => [],
  ];
  array_unshift($element['#element_validate'], [
    get_called_class(),
    'validateWebformOptionsCustom',
  ]);

  // Attach libraries.
  $element['#attached']['library'][] = 'webform_options_custom/webform_options_custom.element';
  if ($element['#zoom']) {
    $element['#attached']['library'][] = 'webform_options_custom/libraries.svg-pan-zoom';
  }

  // Process states.
  webform_process_states($element, '#wrapper_attributes');
  return $element;
}