You are here

protected function ConditionalFieldsFormHelper::mapStates in Conditional Fields 8

Same name and namespace in other branches
  1. 4.x src/ConditionalFieldsFormHelper.php \Drupal\conditional_fields\ConditionalFieldsFormHelper::mapStates()

Map the states based on the conjunctions.

1 call to ConditionalFieldsFormHelper::mapStates()
ConditionalFieldsFormHelper::processDependentFields in src/ConditionalFieldsFormHelper.php
Build and attach #states properties to dependent fields.

File

src/ConditionalFieldsFormHelper.php, line 754

Class

ConditionalFieldsFormHelper
Helper to interact with forms.

Namespace

Drupal\conditional_fields

Code

protected function mapStates() {
  $states_new = [];
  foreach ($this->states as $state_key => $value) {

    // As the main object is ANDed together we can add the AND items directly.
    if (!empty($this->states[$state_key]['AND'])) {
      $states_new[$state_key] = $this->states[$state_key]['AND'];
    }

    // The OR and XOR groups are moved into a sub-array that has numeric keys
    // so that we get a JSON array and not an object, as required by the States
    // API for OR and XOR groupings.
    if (!empty($this->states[$state_key]['OR'])) {
      $or = [];
      foreach ($this->states[$state_key]['OR'] as $constraint_key => $constraint_value) {
        $or[] = [
          $constraint_key => $constraint_value,
        ];
      }

      // '1' as a string so that we get an object (which means logic groups
      // are ANDed together).
      $states_new[$state_key]['1'] = $or;
    }
    if (!empty($this->states[$state_key]['XOR'])) {
      $xor = [
        'xor',
      ];
      foreach ($this->states[$state_key]['XOR'] as $constraint_key => $constraint_value) {
        $xor[] = [
          $constraint_key => $constraint_value,
        ];
      }

      // '2' as a string so that we get an object.
      $states_new[$state_key]['2'] = $xor;
    }
  }
  return $states_new;
}