You are here

public function DefaultStateHandler::statesHandler in Conditional Fields 8

Same name and namespace in other branches
  1. 4.x src/Plugin/conditional_fields/handler/DefaultStateHandler.php \Drupal\conditional_fields\Plugin\conditional_fields\handler\DefaultStateHandler::statesHandler()

Executes states handler according to conditional fields settings.

Overrides ConditionalFieldsHandlersPluginInterface::statesHandler

File

src/Plugin/conditional_fields/handler/DefaultStateHandler.php, line 21

Class

DefaultStateHandler
Provides states handler for text fields.

Namespace

Drupal\conditional_fields\Plugin\conditional_fields\handler

Code

public function statesHandler($field, $field_info, $options) {

  // Build the values that trigger the dependency.
  $values = [];
  $values_array = $this
    ->getConditionValues($options);
  $values_set = $options['values_set'];
  switch ($values_set) {
    case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET:
      $values[$options['condition']] = $options['value_form'];
      break;
    case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX:
      $values[$options['condition']] = [
        'regex' => $options['regex'],
      ];
      break;
    case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR:
      $values[$options['condition']] = [
        'xor' => $values_array,
      ];
      break;
    case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND:
      $values[$options['condition']] = count($values_array) == 1 ? $values_array[0] : $values_array;
      break;
    default:
      if ($options['values_set'] == ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT) {
        $options['state'] = '!' . $options['state'];
      }

      // OR, NOT conditions are obtained with a nested array.
      if (!empty($values_array)) {
        foreach ($values_array as $value) {
          $values[] = [
            'value' => $value,
          ];
        }
      }
      else {
        $values = $options['values'];
      }
      break;
  }
  $state = [
    $options['state'] => [
      $options['selector'] => $values,
    ],
  ];
  return $state;
}