protected function OptionsButtons::checkBoxesHandler in Conditional Fields 8
Same name and namespace in other branches
- 4.x src/Plugin/conditional_fields/handler/OptionsButtons.php \Drupal\conditional_fields\Plugin\conditional_fields\handler\OptionsButtons::checkBoxesHandler()
Return state for check boxes.
1 call to OptionsButtons::checkBoxesHandler()
- 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 104
Class
- OptionsButtons
- Provides states handler for Check boxes/radio buttons.
Namespace
Drupal\conditional_fields\Plugin\conditional_fields\handlerCode
protected function checkBoxesHandler($field, $field_info, $options) {
// Checkboxes are actually different form fields, so the #states property
// has to include a state for each checkbox.
$checkboxes_selectors = [];
$state = [];
$values_array = $this
->getConditionValues($options);
$select = conditional_fields_field_selector($field);
switch ($options['values_set']) {
case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET:
$values_array = $this
->getWidgetValue($options['value_form']);
//We are placed on the parent field with options
if (isset($field['#options'])) {
foreach ($field['#options'] as $id => $label) {
if (isset($field[$id]) && is_array($field[$id])) {
$selector_key = conditional_fields_field_selector($field[$id]);
if (!$selector_key) {
$selector_key = sprintf("[name=\"%s\"]", $this
->getFieldName($field));
}
}
else {
$selector_key = $select;
}
$checkboxes_selectors[$selector_key] = [
'checked' => in_array($id, $values_array),
];
}
}
elseif (isset($field['#return_value'])) {
//We are placed inside the option of the checkboxes
$selector = conditional_fields_field_selector($field);
foreach ($options['value_form'] as $value) {
$selector_key = str_replace($field['#return_value'], current($value), $selector);
$checkboxes_selectors[$selector_key] = [
'checked' => true,
];
}
}
$state[$options['state']] = $checkboxes_selectors;
break;
case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX:
// We interpret this as: checkboxes whose values match the regular
// expression should be checked.
if (isset($field['#options'])) {
foreach ($field['#options'] as $key => $label) {
if (preg_match('/' . $options['regex'] . '/', $key)) {
$checkboxes_selectors = [
conditional_fields_field_selector($field[$key]) => [
'checked' => true,
],
];
$state[$options['state']][] = $checkboxes_selectors;
}
}
}
break;
case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND:
if (!empty($values_array)) {
foreach ($values_array as $value) {
if (isset($field[$value])) {
$checkboxes_selectors[conditional_fields_field_selector($field[$value])] = [
'checked' => true,
];
}
}
}
else {
$checkboxes_selectors[conditional_fields_field_selector($field[$options['values']])] = [
'checked' => true,
];
}
$state[$options['state']] = $checkboxes_selectors;
break;
case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR:
foreach ($values_array as $index => $value) {
if ($index > 0) {
$checkboxes_selectors[] = 'xor';
}
$checkboxes_selectors[] = [
conditional_fields_field_selector($field[$value]) => [
'checked' => true,
],
];
}
$state[$options['state']] = $checkboxes_selectors;
break;
case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT:
$options['state'] = '!' . $options['state'];
case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR:
foreach ($values_array as $value) {
$checkboxes_selectors[] = [
conditional_fields_field_selector($field[$value]) => [
'checked' => true,
],
];
}
$state[$options['state']] = $checkboxes_selectors;
break;
}
return $state;
}