You are here

public static function WebformElementHelper::fixStatesWrapper in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Utility/WebformElementHelper.php \Drupal\webform\Utility\WebformElementHelper::fixStatesWrapper()

Fix webform element #states handling.

Parameters

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

7 calls to WebformElementHelper::fixStatesWrapper()
WebformAdminConfigBaseForm::buildBulkOperations in src/Form/AdminConfig/WebformAdminConfigBaseForm.php
Build bulk operation settings for webforms and submissions.
WebformComputedBase::processWebformComputed in src/Element/WebformComputedBase.php
Processes a Webform computed token element.
WebformElementBase::preRenderFixStatesWrapper in src/Plugin/WebformElementBase.php
Fix state wrapper.
WebformHtmlEditor::processWebformHtmlEditor in src/Element/WebformHtmlEditor.php
Prepares a #type 'webform_html_editor' render element for input.html.twig.
WebformImageSelectImages::processWebformImageSelectImages in modules/webform_image_select/src/Element/WebformImageSelectImages.php
Process images and build images widget.

... See full list

File

src/Utility/WebformElementHelper.php, line 342

Class

WebformElementHelper
Helper class webform element methods.

Namespace

Drupal\webform\Utility

Code

public static function fixStatesWrapper(array &$element) {
  $attributes = [];

  // Add .js-webform-states-hidden to hide elements when they are being rendered.
  $attributes_properties = [
    '#wrapper_attributes',
    '#attributes',
  ];
  foreach ($attributes_properties as $attributes_property) {
    if (isset($element[$attributes_property]) && isset($element[$attributes_property]['class'])) {
      $index = array_search('js-webform-states-hidden', $element[$attributes_property]['class']);
      if ($index !== FALSE) {
        unset($element[$attributes_property]['class'][$index]);
        $attributes['class'][] = 'js-webform-states-hidden';
        break;
      }
    }
  }

  // Do not add wrapper if there is no #states and
  // is no .js-webform-states-hidden class.
  if (empty($element['#states']) && empty($attributes)) {
    return;
  }

  // Set .js-form-wrapper which is targeted by states.js hide/show logic.
  $attributes['class'][] = 'js-form-wrapper';

  // Move the element's #states the wrapper's #states.
  if (isset($element['#states'])) {
    $attributes['data-drupal-states'] = Json::encode($element['#states']);

    // Copy #states to #_webform_states property which can be used by the
    // WebformSubmissionConditionsValidator.
    // @see \Drupal\webform\WebformSubmissionConditionsValidator
    $element['#_webform_states'] = $element['#states'];

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

  // If there are attributes for the wrapper do not add it.
  if (empty($attributes)) {
    return;
  }
  $element += [
    '#prefix' => '',
    '#suffix' => '',
  ];
  $element['#prefix'] = '<div' . new Attribute($attributes) . '>' . $element['#prefix'];
  $element['#suffix'] = $element['#suffix'] . '</div>';

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