You are here

protected static function ConditionalFieldsFormHelper::evaluateDependencies in Conditional Fields 8

Same name and namespace in other branches
  1. 4.x src/ConditionalFieldsFormHelper.php \Drupal\conditional_fields\ConditionalFieldsFormHelper::evaluateDependencies()

Evaluate a set of dependencies for a dependent field.

Parameters

array $dependent: The field form element in the current language.

Return value

array|bool $evaluated_dependees. Evaluated dependencies array.

1 call to ConditionalFieldsFormHelper::evaluateDependencies()
ConditionalFieldsFormHelper::dependentValidate in src/ConditionalFieldsFormHelper.php
Dependent field validation callback.

File

src/ConditionalFieldsFormHelper.php, line 417

Class

ConditionalFieldsFormHelper
Helper to interact with forms.

Namespace

Drupal\conditional_fields

Code

protected static function evaluateDependencies($dependent, $form, $form_state, $grouping = TRUE) {
  $dependencies = $form['#conditional_fields'][reset($dependent['field_parents'])]['dependees'];
  $evaluated_dependees = [];
  foreach ($dependencies as $dependency_id => $dependency) {

    // Extract field values from submitted values.
    $dependee = $dependency['dependee'];

    // Skip any misconfigured conditions.
    if (empty($form['#conditional_fields'][$dependee]['parents'])) {
      continue;
    }
    $dependee_parents = $form['#conditional_fields'][$dependee]['parents'];

    // We have the parents of the field, but depending on the entity type and
    // the widget type, they may include additional elements that are actually
    // part of the value. So we find the depth of the field inside the form
    // structure and use the parents only up to that depth.
    $dependee_parents_keys = array_flip($dependee_parents);
    $dependee_parent = NestedArray::getValue($form, array_slice($dependee_parents, 0, $dependee_parents_keys[$dependee]));
    $values = self::formFieldGetValues($dependee_parent[$dependee], $form_state);

    // Remove the language key.
    if (isset($dependee_parent[$dependee]['#language'], $values[$dependee_parent[$dependee]['#language']])) {
      $values = $values[$dependee_parent[$dependee]['#language']];
    }
    if ($grouping) {
      $evaluated_dependees[reset($dependent['field_parents'])][$dependency['options']['grouping']][] = self::evaluateDependency('edit', $values, $dependency['options']);
    }
    else {
      $evaluated_dependees[reset($dependent['field_parents'])][$dependency['options']['grouping']][$dependency['options']['state']] = self::evaluateDependency('edit', $values, $dependency['options']);
    }
  }
  if ($grouping) {
    return self::evaluateGrouping($evaluated_dependees[reset($dependent['field_parents'])]);
  }
  return $evaluated_dependees;
}