You are here

protected static function ConditionalFieldsFormHelper::evaluateDependencies in Conditional Fields 4.x

Same name and namespace in other branches
  1. 8 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.

array $form: The form to evaluate dependencies on.

\Drupal\Core\Form\FormStateInterface $form_state: The state of the form to evaluate dependencies on.

bool $grouping: TRUE to evaluate grouping; FALSE otherwise.

Return value

array|bool Evaluated dependencies array.

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

File

src/ConditionalFieldsFormHelper.php, line 489

Class

ConditionalFieldsFormHelper
Helper to interact with forms.

Namespace

Drupal\conditional_fields

Code

protected static function evaluateDependencies(array $dependent, array $form, FormStateInterface $form_state, $grouping = TRUE) {
  $dependencies = $form['#conditional_fields'][reset($dependent['field_parents'])]['dependees'];
  $evaluated_dependees = [];
  foreach ($dependencies as $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);
    if (isset($values['value']) && is_numeric($values['value'])) {
      $values = $values['value'];
    }

    // 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;
}