You are here

protected static function YamlFormElementStates::convertFormApiStatesToStatesArray in YAML Form 8

Convert Form API #states to states array.

Parameters

array $fapi_states: An associative array containing Form API #states.

Return value

array An associative array of states.

2 calls to YamlFormElementStates::convertFormApiStatesToStatesArray()
YamlFormElementStates::processYamlFormStates in src/Element/YamlFormElementStates.php
Expand an email confirm field into two HTML5 email elements.
YamlFormElementStates::valueCallback in src/Element/YamlFormElementStates.php
Determines how user input is mapped to an element's #value property.

File

src/Element/YamlFormElementStates.php, line 517

Class

YamlFormElementStates
Provides a form element to edit an element's #states.

Namespace

Drupal\yamlform\Element

Code

protected static function convertFormApiStatesToStatesArray(array $fapi_states) {
  $index = 0;
  $states = [];
  foreach ($fapi_states as $state => $conditions) {
    $states[$index] = [
      'state' => $state,
      'operator' => 'and',
      'conditions' => [],
    ];
    foreach ($conditions as $condition_key => $condition_value) {
      if (is_string($condition_key)) {
        $states[$index]['conditions'][] = [
          'selector' => $condition_key,
          'trigger' => key($condition_value),
          'value' => reset($condition_value),
        ];
      }
      elseif (is_string($condition_value)) {
        $states[$index]['operator'] = $condition_value;
      }
      else {
        foreach ($condition_value as $subcondition_key => $subcondition_value) {
          $states[$index]['conditions'][] = [
            'selector' => $subcondition_key,
            'trigger' => key($subcondition_value),
            'value' => reset($subcondition_value),
          ];
        }
      }
    }
    $index++;
  }
  return $states;
}