You are here

public static function YamlFormOtherBase::processYamlFormOther in YAML Form 8

Processes an 'other' element.

See select list form element for select list properties.

See also

\Drupal\Core\Render\Element\Select

1 call to YamlFormOtherBase::processYamlFormOther()
YamlFormButtonsOther::processYamlFormOther in src/Element/YamlFormButtonsOther.php
Processes an 'other' element.
1 method overrides YamlFormOtherBase::processYamlFormOther()
YamlFormButtonsOther::processYamlFormOther in src/Element/YamlFormButtonsOther.php
Processes an 'other' element.

File

src/Element/YamlFormOtherBase.php, line 78

Class

YamlFormOtherBase
Base class for form other element.

Namespace

Drupal\yamlform\Element

Code

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

  // Remove 'yamlform_' prefix from type.
  $type = str_replace('yamlform_', '', static::$type);
  $properties = static::$properties;
  $element['#tree'] = TRUE;
  $element['#wrapper_attributes']['class'][] = "js-yamlform-{$type}-other";
  $element['#wrapper_attributes']['class'][] = "yamlform-{$type}-other";
  $element[$type]['#type'] = static::$type;
  $element[$type] += array_intersect_key($element, array_combine($properties, $properties));
  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']['#error_no_message'] = TRUE;
  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',
    '#placeholder' => t('Enter other...'),
  ];
  $element['other']['#wrapper_attributes']['class'][] = "js-yamlform-{$type}-other-input";
  $element['other']['#wrapper_attributes']['class'][] = "yamlform-{$type}-other-input";

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

  // Set validation.
  if (isset($element['#element_validate'])) {
    $element['#element_validate'] = array_merge([
      [
        get_called_class(),
        'validateYamlFormOther',
      ],
    ], $element['#element_validate']);
  }
  else {
    $element['#element_validate'] = [
      [
        get_called_class(),
        'validateYamlFormOther',
      ],
    ];
  }

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

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