Checkbox.php in Conditional Fields 4.x
File
src/Plugin/conditional_fields/handler/Checkbox.php
View source
<?php
namespace Drupal\conditional_fields\Plugin\conditional_fields\handler;
use Drupal\conditional_fields\ConditionalFieldsHandlerBase;
use Drupal\conditional_fields\ConditionalFieldsInterface;
class Checkbox extends ConditionalFieldsHandlerBase {
public function statesHandler($field, $field_info, $options) {
$state = [];
$checked = FALSE;
switch ($options['values_set']) {
case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET:
$widget_value = $this
->getWidgetValue($options['value_form']);
$checked = $field['#return_value'] == $widget_value;
break;
case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX:
$checked = preg_match('/' . $options['value']['RegExp'] . '/', $field['#on_value']) ? TRUE : FALSE;
break;
case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND:
$checked = $options['values'][0] == $field['#on_value'] ? TRUE : FALSE;
break;
case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR:
case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR:
case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT:
$checked = in_array($field['#on_value'], $options['values']) ? TRUE : FALSE;
break;
}
$state[$options['state']][$options['selector']] = [
'checked' => $checked,
];
return $state;
}
public function getWidgetValue(array $value_form) {
return isset($value_form[0]['value']) ? $value_form[0]['value'] : $value_form;
}
}
Classes
Name |
Description |
Checkbox |
Provides states handler for single on/off checkbox. |