View source
<?php
namespace Drupal\conditional_fields\Plugin\conditional_fields\handler;
use Drupal\conditional_fields\ConditionalFieldsHandlerBase;
use Drupal\conditional_fields\ConditionalFieldsInterface;
class OptionsButtons extends ConditionalFieldsHandlerBase {
public function statesHandler($field, $field_info, $options) {
if (array_key_exists('#type', $field) && in_array($field['#type'], [
'checkbox',
'checkboxes',
])) {
return $this
->checkBoxesHandler($field, $field_info, $options);
}
elseif (array_key_exists('#type', $field) && in_array($field['#type'], [
'radio',
'radios',
])) {
return $this
->radioHandler($field, $field_info, $options);
}
return [];
}
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:
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)) {
$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:
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;
}
protected function checkBoxesHandler($field, $field_info, $options) {
$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']);
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'])) {
$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:
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;
}
public function getFieldName(array $field) {
$field_name = false;
if (isset($field['#name'])) {
$field_name = $field['#name'];
}
elseif (isset($field['#field_name'])) {
$field_name = $field['#field_name'];
}
elseif (isset($field['#array_parents']) && !empty($field['#array_parents'])) {
$field_name = $field['#parents'][0];
}
elseif (isset($field['#parents']) && is_array($field['#parents'])) {
$field_name = $field['#parents'][0];
}
return $field_name;
}
public function getWidgetValue(array $value_form) {
$values = [];
if (empty($value_form)) {
return $values;
}
else {
foreach ($value_form as $value) {
if (isset($value['value'])) {
$values[] = $value['value'];
}
elseif (isset($value['target_id'])) {
$values[] = $value['target_id'];
}
elseif (isset($value['nid'])) {
$values[] = $value['nid'];
}
elseif (isset($value['vid'])) {
$values[] = $value['vid'];
}
elseif (isset($value['uid'])) {
$values[] = $value['uid'];
}
elseif (isset($value['fid'])) {
$values[] = $value['fid'];
}
elseif (isset($value['id'])) {
$values[] = $value['id'];
}
else {
$values[] = $value;
}
}
return $values;
}
}
}