function conditional_fields_evaluate_dependency in Conditional Fields 7.3
Evaluate if a dependency meets the requirements to be triggered.
Parameters
$context: 'edit' if $values are extracted from $form_state or 'view' if $values are extracted from an entity.
2 calls to conditional_fields_evaluate_dependency()
- conditional_fields_entity_view_alter in ./
conditional_fields.module - Implements hook_entity_view_alter().
- conditional_fields_evaluate_dependencies in ./
conditional_fields.module - Evaluate a set of dependencies for a dependent field.
File
- ./
conditional_fields.module, line 1049 - Define dependencies between fields based on their states and values.
Code
function conditional_fields_evaluate_dependency($context, $values, $options) {
if ($options['condition'] == 'empty') {
$options['values_set'] = CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND;
$options['values'] = array(
'',
);
}
elseif ($options['condition'] == '!empty') {
$options['values_set'] = CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT;
$options['values'] = array(
'',
);
}
if ($options['values_set'] == CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET) {
$dependency_values = $context == 'view' ? $options['value'] : $options['value_form'];
// Simple case: both values are strings or integers. Should never happen in
// view context, but does no harm to check anyway.
if (!is_array($values)) {
// Options elements consider "_none" value same as empty.
$values = $values === '_none' ? '' : $values;
if (!is_array($dependency_values)) {
// Some widgets store integers, but values saved in $dependency_values
// are always strings. Convert them to integers because we want to do a
// strict equality check to differentiate empty strings from '0'.
if (is_int($values) && is_numeric($dependency_values)) {
$dependency_values = (int) $dependency_values;
}
return $dependency_values === $values;
}
// If $values is a string and $dependency_values an array, convert $values
// to the standard field array form format. This covers cases like single
// value textfields.
$values = array(
array(
'value' => $values,
),
);
}
// If we are in form context, we are almost done.
if ($context == 'edit') {
// If $dependency_values is not an array, we can only assume that it
// should map to the first key of the first value of $values.
if (!is_array($dependency_values)) {
$key = current(array_keys((array) current($values)));
$dependency_values = array(
array(
$key => $dependency_values,
),
);
}
// Compare arrays recursively ignoring keys, since multiple select widgets
// values have numeric keys in form format and string keys in storage
// format.
return array_values($dependency_values) == array_values($values);
}
// $values, when viewing fields, may contain all sort of additional
// information, so filter out from $values the keys that are not present in
// $dependency_values.
// Values here are alway keyed by delta (regardless of multiple value
// settings).
foreach ($values as $delta => &$value) {
if (isset($dependency_values[$delta])) {
$value = array_intersect_key($value, $dependency_values[$delta]);
foreach ($value as $key => &$element_value) {
if (isset($dependency_values[$delta][$key]) && is_int($dependency_values[$delta][$key]) && is_numeric($element_value)) {
$element_value = (int) $element_value;
}
}
}
}
// Compare values.
foreach ($dependency_values as $delta => $dependency_value) {
if (!isset($values[$delta])) {
return FALSE;
}
foreach ($dependency_value as $key => $dependency_element_value) {
// Ignore keys set in the field and not in the dependency.
if (isset($values[$delta][$key]) && $values[$delta][$key] !== $dependency_element_value) {
return FALSE;
}
}
}
return TRUE;
}
// Flatten array of values.
$reference_values = array();
foreach ((array) $values as $value) {
// TODO: support multiple values.
$reference_values[] = is_array($value) ? array_shift($value) : $value;
}
// Regular expression method.
if ($options['values_set'] == CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX) {
foreach ($reference_values as $reference_value) {
if (!preg_match('/' . $options['value']['RegExp'] . '/', $reference_value)) {
return FALSE;
}
}
return TRUE;
}
switch ($options['values_set']) {
case CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND:
$diff = array_diff($options['values'], $reference_values);
return empty($diff);
case CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR:
$intersect = array_intersect($options['values'], $reference_values);
return !empty($intersect);
case CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR:
$intersect = array_intersect($options['values'], $reference_values);
return count($intersect) == 1;
case CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT:
$intersect = array_intersect($options['values'], $reference_values);
return empty($intersect);
}
}