You are here

protected static function YamlFormElementStates::convertStatesArrayToFormApiStates in YAML Form 8

Convert states array to Form API #states.

Parameters

array $states_array: An associative array containing states.

Return value

array An associative array of states.

1 call to YamlFormElementStates::convertStatesArrayToFormApiStates()
YamlFormElementStates::convertFormValuesToFormApiStates in src/Element/YamlFormElementStates.php
Convert form values to states array.

File

src/Element/YamlFormElementStates.php, line 562

Class

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

Namespace

Drupal\yamlform\Element

Code

protected static function convertStatesArrayToFormApiStates(array $states_array = []) {
  $states = [];
  foreach ($states_array as $state_array) {
    if ($state = $state_array['state']) {
      $operator = $state_array['operator'];
      $conditions = $state_array['conditions'];
      if (count($conditions) === 1) {
        $condition = reset($conditions);
        $selector = $condition['selector'];
        $trigger = $condition['trigger'];
        if ($selector && $trigger) {
          $value = $condition['value'] ?: TRUE;
        }
        else {
          $value = '';
        }
        $states[$state][$selector][$trigger] = $value;
      }
      else {
        foreach ($state_array['conditions'] as $index => $condition) {
          $selector = $condition['selector'];
          $trigger = $condition['trigger'];
          $value = $condition['value'] ?: TRUE;
          if ($selector && $trigger) {
            if ($index !== 0 && $operator == 'or') {
              $states[$state][] = $operator;
            }
            $states[$state][] = [
              $selector => [
                $trigger => $value,
              ],
            ];
          }
        }
      }
    }
  }
  return $states;
}