You are here

public static function WebformOtherBase::processWebformOther in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Element/WebformOtherBase.php \Drupal\webform\Element\WebformOtherBase::processWebformOther()

Processes an 'other' element.

See select list webform element for select list properties.

See also

\Drupal\Core\Render\Element\Select

1 call to WebformOtherBase::processWebformOther()
WebformButtonsOther::processWebformOther in modules/webform_jqueryui_buttons/src/Element/WebformButtonsOther.php
Processes an 'other' element.
1 method overrides WebformOtherBase::processWebformOther()
WebformButtonsOther::processWebformOther in modules/webform_jqueryui_buttons/src/Element/WebformButtonsOther.php
Processes an 'other' element.

File

src/Element/WebformOtherBase.php, line 112

Class

WebformOtherBase
Base class for webform other element.

Namespace

Drupal\webform\Element

Code

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

  // Remove 'webform_' prefix from type.
  $type = str_replace('webform_', '', static::$type);
  $properties = static::$properties;
  $element['#tree'] = TRUE;
  $element[$type]['#type'] = static::$type;
  $element[$type]['#webform_element'] = TRUE;
  $element[$type]['#webform_other'] = TRUE;
  $element[$type] += array_intersect_key($element, array_combine($properties, $properties));
  $element[$type]['#title_display'] = 'invisible';
  if (!isset($element[$type]['#options'][static::OTHER_OPTION])) {
    $element[$type]['#options'][static::OTHER_OPTION] = !empty($element['#other__option_label']) ? $element['#other__option_label'] : t('Other…');
  }
  $element[$type]['#error_no_message'] = TRUE;

  // Build other textfield.
  $element += [
    'other' => [],
  ];
  foreach ($element as $key => $value) {
    if (strpos($key, '#other__') === 0) {
      $other_key = str_replace('#other__', '#', $key);
      if (!isset($element['other'][$other_key])) {
        $element['other'][$other_key] = $value;
      }
    }
  }
  $element['other'] += [
    '#type' => 'textfield',
    '#webform_element' => TRUE,
    '#placeholder' => t('Enter other…'),
  ];
  if (!isset($element['other']['#title'])) {
    $element['other'] += [
      '#title' => $element['other']['#placeholder'],
      '#title_display' => 'invisible',
    ];
  }
  $element['other'] += array_intersect_key($element, array_combine(static::$otherProperties, static::$otherProperties));
  $element['other']['#wrapper_attributes']['class'][] = "js-webform-{$type}-other-input";
  $element['other']['#wrapper_attributes']['class'][] = "webform-{$type}-other-input";
  if ($element['other']['#type'] === 'datetime') {
    $element['other']['#prefix'] = '<div class="' . implode(' ', $element['other']['#wrapper_attributes']['class']) . '">';
    $element['other']['#suffix'] = '</div>';
    unset($element['other']['#wrapper_attributes']['class']);
  }

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

  // Add custom required error message so that clientside_validation.module
  // can display it.
  // @see https://www.drupal.org/project/clientside_validation/issues/3084798
  if (\Drupal::moduleHandler()
    ->moduleExists('clientside_validation') && isset($element['other']['#required_error'])) {
    $element['other']['#attributes']['data-msg-required'] = $element['other']['#required_error'];
  }

  // Initialize the type and other elements to allow for webform enhancements.

  /** @var \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager */
  $element_manager = \Drupal::service('plugin.manager.webform.element');
  $element_manager
    ->buildElement($element[$type], $complete_form, $form_state);
  $element_manager
    ->buildElement($element['other'], $complete_form, $form_state);

  // Prevent nested fieldset by removing fieldset theme wrapper around
  // radios and checkboxes.
  // @see \Drupal\Core\Render\Element\CompositeFormElementTrait
  $element[$type]['#pre_render'] = [];

  // Add js trigger attributes to the composite wrapper.
  // @see \Drupal\webform\Element\WebformCompositeFormElementTrait
  $is_form_element_wrapper = isset($element['#wrapper_type']) && $element['#wrapper_type'] === 'form_element';
  $wrapper_attributes = $is_form_element_wrapper ? '#wrapper_attributes' : '#attributes';
  $element[$wrapper_attributes]['class'][] = "js-webform-{$type}-other";
  $element[$wrapper_attributes]['class'][] = "webform-{$type}-other";

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

  // Make sure form element label has no 'for' attribute.
  $element['#label_attributes']['webform-remove-for-attribute'] = TRUE;

  // Remove options.
  unset($element['#options']);

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

  // Attach library.
  $element['#attached']['library'][] = 'webform/webform.element.other';

  // Process states.
  WebformFormHelper::processStates($element, '#wrapper_attributes');
  return $element;
}