You are here

protected static function WebformElementStates::convertElementValueToFormApiStates in Webform 6.x

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

Convert an element's submitted value to Form API #states.

Parameters

array $element: The form element.

array $errors: An array used to capture errors.

Return value

array An associative array of states.

1 call to WebformElementStates::convertElementValueToFormApiStates()
WebformElementStates::validateWebformElementStates in src/Element/WebformElementStates.php
Validates webform states element.

File

src/Element/WebformElementStates.php, line 834

Class

WebformElementStates
Provides a webform element to edit an element's #states.

Namespace

Drupal\webform\Element

Code

protected static function convertElementValueToFormApiStates(array $element, array &$errors = []) {
  $states = [];
  $states_array = static::convertFormValuesToStatesArray($element['states']['#value']);
  foreach ($states_array as $state_array) {
    $state = $state_array['state'];
    if (!$state) {
      continue;
    }

    // Check for duplicate states.
    if (isset($states[$state])) {
      static::setFormApiStateError($element, $errors, $state);
    }

    // Define values extracted from
    // WebformElementStates::getFormApiStatesCondition().
    $selector = NULL;
    $trigger = NULL;
    $value = NULL;
    $operator = $state_array['operator'];
    $conditions = $state_array['conditions'];
    if (count($conditions) === 1) {
      $condition = reset($conditions);
      extract(static::getFormApiStatesCondition($condition));

      // Check for duplicate selectors.
      if (isset($states[$state][$selector])) {
        static::setFormApiStateError($element, $errors, $state, $selector);
      }
      $states[$state][$selector][$trigger] = $value;
    }
    else {
      foreach ($state_array['conditions'] as $index => $condition) {
        extract(static::getFormApiStatesCondition($condition));
        if ($selector && $trigger) {
          if ($operator === 'or' || $operator === 'xor') {
            if ($index !== 0) {
              $states[$state][] = $operator;
            }
            $states[$state][] = [
              $selector => [
                $trigger => $value,
              ],
            ];
          }
          else {

            // Check for duplicate selectors.
            if (isset($states[$state][$selector])) {
              static::setFormApiStateError($element, $errors, $state, $selector);
            }
            $states[$state][$selector] = [
              $trigger => $value,
            ];
          }
        }
      }
    }
  }
  return $states;
}