You are here

protected function OptionsButtons::radioHandler in Conditional Fields 8

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

Return state for radio.

1 call to OptionsButtons::radioHandler()
OptionsButtons::statesHandler in src/Plugin/conditional_fields/handler/OptionsButtons.php
Executes states handler according to conditional fields settings.

File

src/Plugin/conditional_fields/handler/OptionsButtons.php, line 35

Class

OptionsButtons
Provides states handler for Check boxes/radio buttons.

Namespace

Drupal\conditional_fields\Plugin\conditional_fields\handler

Code

protected function radioHandler($field, $field_info, $options) {
  $select_states = [];
  $values_array = $this
    ->getConditionValues($options);
  $state = [];
  switch ($options['values_set']) {
    case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET:

      // TODO: Try to get key_column automatically.
      // like here:
      // @see \Drupal\conditional_fields\Plugin\conditional_fields\handler\Select::widgetCase()
      if (isset($options['value_form'][0]['value'])) {
        $column_key = 'value';
      }
      elseif (isset($options['value_form'][0]['target_id'])) {
        $column_key = 'target_id';
      }
      else {
        break;
      }
      $select_states[$options['selector']] = [
        $options['condition'] => $options['value_form'][0][$column_key],
      ];
      $state = [
        $options['state'] => $select_states,
      ];
      break;
    case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND:
      if (is_array($values_array)) {

        // Will take the first value
        // because there is no possibility to choose more with radio buttons.
        $select_states[$options['selector']] = [
          $options['condition'] => $values_array[0],
        ];
      }
      else {
        $select_states[$options['selector']] = [
          $options['condition'] => $values_array,
        ];
      }
      $state = [
        $options['state'] => $select_states,
      ];
      break;
    case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX:

      // This just works.
      break;
    case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR:
      $select_states[$options['selector']] = [
        $options['condition'] => [
          'xor' => $values_array,
        ],
      ];
      $state = [
        $options['state'] => $select_states,
      ];
      break;
    case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT:
      $options['state'] = '!' . $options['state'];
    case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR:
      if (is_array($values_array)) {
        foreach ($values_array as $value) {
          $select_states[$options['selector']][] = [
            $options['condition'] => $value,
          ];
        }
      }
      else {
        $select_states[$options['selector']] = [
          $options['condition'] => $values_array,
        ];
      }
      $state = [
        $options['state'] => $select_states,
      ];
      break;
  }
  return $state;
}