You are here

public static function YamlFormElementHelper::fixStatesWrapper in YAML Form 8

Fix form element #states handling.

Parameters

array $element: A form element that is missing the 'data-drupal-states' attribute.

3 calls to YamlFormElementHelper::fixStatesWrapper()
YamlFormElementBase::prepareWrapper in src/YamlFormElementBase.php
Set an elements Flexbox and #states wrapper.
YamlFormMultiple::processYamlFormMultiple in src/Element/YamlFormMultiple.php
Process items and build multiple elements widget.
YamlFormOptions::processYamlFormOptions in src/Element/YamlFormOptions.php
Process options and build options widget.

File

src/Utility/YamlFormElementHelper.php, line 104

Class

YamlFormElementHelper
Helper class form element methods.

Namespace

Drupal\yamlform\Utility

Code

public static function fixStatesWrapper(array &$element) {
  if (empty($element['#states'])) {
    return;
  }
  $attributes = [];
  $attributes['class'][] = 'js-form-wrapper';
  $attributes['data-drupal-states'] = Json::encode($element['#states']);
  $element += [
    '#prefix' => '',
    '#suffix' => '',
  ];

  // ISSUE: JSON is being corrupted when the prefix is rendered.
  // $element['#prefix'] = '<div ' . new Attribute($attributes) . '>' . $element['#prefix'];
  // WORKAROUND: Safely set filtered #prefix to FormattableMarkup.
  $allowed_tags = isset($element['#allowed_tags']) ? $element['#allowed_tags'] : Xss::getHtmlTagList();
  $element['#prefix'] = new FormattableMarkup('<div' . new Attribute($attributes) . '>' . Xss::filter($element['#prefix'], $allowed_tags), []);
  $element['#suffix'] = $element['#suffix'] . '</div>';

  // Attach library.
  $element['#attached']['library'][] = 'core/drupal.states';

  // Remove #states property to prevent nesting.
  unset($element['#states']);
}