Select.php in Conditional Fields 4.x
File
src/Plugin/conditional_fields/handler/Select.php
View source
<?php
namespace Drupal\conditional_fields\Plugin\conditional_fields\handler;
use Drupal\conditional_fields\ConditionalFieldsHandlerBase;
use Drupal\conditional_fields\ConditionalFieldsInterface;
class Select extends ConditionalFieldsHandlerBase {
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;
}
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;
}
}
Classes
Name |
Description |
Select |
Provides states handler for multiple select lists. |