You are here

class Select in Conditional Fields 4.x

Same name and namespace in other branches
  1. 8 src/Plugin/conditional_fields/handler/Select.php \Drupal\conditional_fields\Plugin\conditional_fields\handler\Select

Provides states handler for multiple select lists.

Multiple select fields always require an array as value. In addition, since our modified States API triggers a dependency only if all reference values of type Array are selected, a different selector must be added for each value of a set for OR, XOR and NOT evaluations.

Plugin annotation


@ConditionalFieldsHandler(
  id = "states_handler_options_select",
)

Hierarchy

Expanded class hierarchy of Select

File

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

Namespace

Drupal\conditional_fields\Plugin\conditional_fields\handler
View source
class Select extends ConditionalFieldsHandlerBase {

  /**
   * {@inheritdoc}
   */
  public function statesHandler($field, $field_info, $options) {
    $state = [];
    $select_states = [];
    $values_array = $this
      ->getConditionValues($options);
    switch ($options['values_set']) {
      case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET:
        return $this
          ->widgetCase($field, $options);
      case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND:
        if (isset($state[$options['state']][$options['selector']]['value'])) {
          $state[$options['state']][$options['selector']]['value'] = (array) $state[$options['state']][$options['selector']]['value'];
        }
        else {
          $state[$options['state']][$options['selector']]['value'] = $values_array;
        }
        break;
      case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR:
        $input_states[$options['selector']] = [
          $options['condition'] => [
            'xor' => $values_array,
          ],
        ];
        $state[$options['state']] = $input_states;
        break;
      case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX:
        $select_states[$options['state']][] = [
          $options['selector'] => [
            $options['condition'] => [
              'regex' => $options['regex'],
            ],
          ],
        ];
        $state = $select_states;
        break;
      case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT:
        $options['state'] = '!' . $options['state'];
      case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR:
        foreach ((array) $options['values'] as $value) {
          $select_states[$options['state']][$options['selector']][] = [
            $options['condition'] => $value,
          ];
        }
        $state = $select_states;
        break;
    }
    return $state;
  }

  /**
   * Returns state in widget input case.
   */
  protected function widgetCase($field, $options) {
    $state = [];
    $key_column = $field['#key_column'];
    if (empty($key_column)) {
      return $state;
    }
    if (!empty($options['value_form'][0][$key_column]) && $options['field_cardinality'] == 1) {
      $state[$options['state']][$options['selector']] = [
        'value' => $options['value_form'][0][$key_column],
      ];
    }
    else {
      $values = array_column($options['value_form'], $key_column);
      $state[$options['state']][$options['selector']] = [
        'value' => $values,
      ];
    }
    return $state;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConditionalFieldsHandlerBase::getConditionValues public function Get values form the condition options. Overrides ConditionalFieldsHandlersPluginInterface::getConditionValues
ConditionalFieldsHandlerBase::getWidgetValue public function Get values from widget settings for plugin. Overrides ConditionalFieldsHandlersPluginInterface::getWidgetValue 5
Select::statesHandler public function Executes states handler according to conditional fields settings. Overrides ConditionalFieldsHandlersPluginInterface::statesHandler
Select::widgetCase protected function Returns state in widget input case.