You are here

public function Number::statesHandler in Conditional Fields 8

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

Executes states handler according to conditional fields settings.

Overrides ConditionalFieldsHandlersPluginInterface::statesHandler

File

src/Plugin/conditional_fields/handler/Number.php, line 20

Class

Number
Provides states handler for Number (integer, decimal and float).

Namespace

Drupal\conditional_fields\Plugin\conditional_fields\handler

Code

public function statesHandler($field, $field_info, $options) {
  $state = [];
  switch ($options['values_set']) {
    case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET:
      if ($options['field_cardinality'] == 1) {
        $state[$options['state']][$options['selector']] = [
          'value' => $this
            ->getWidgetValue($options['value_form']),
        ];
      }
      else {
        $values = array_column($this
          ->getWidgetValue($options['value_form']), 'value');
        foreach ($values as $key => $value) {
          if (empty($value)) {
            continue;
          }
          $selector = str_replace('[0]', "[{$key}]", $options['selector']);
          $state[$options['state']][$selector] = [
            'value' => $value,
          ];
        }
      }
      return $state;
    case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND:

      // Implemented in DefaultStateHandler.
      return $state;
    case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR:
    case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX:
    case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT:
    case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR:

      // Implemented in DefaultStateHandler.
      break;
  }
  return $state;
}