You are here

public function ConditionalFieldsFormHelper::mapStates in Conditional Fields 4.x

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

Map the states based on the conjunctions.

Parameters

array $unmapped_states: An array of unmapped states.

Return value

array An array of mapped states.

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

File

src/ConditionalFieldsFormHelper.php, line 856

Class

ConditionalFieldsFormHelper
Helper to interact with forms.

Namespace

Drupal\conditional_fields

Code

public function mapStates(array $unmapped_states) {
  $states_new = [];
  foreach ($unmapped_states as $state_key => $value) {

    // As the main object is ANDed together we can add the AND items directly.
    if (!empty($unmapped_states[$state_key]['AND'])) {
      $states_new[$state_key] = $unmapped_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($unmapped_states[$state_key]['OR'])) {
      $or = [];
      foreach ($unmapped_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($unmapped_states[$state_key]['XOR'])) {
      $xor = [
        'xor',
      ];
      foreach ($unmapped_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;
}