You are here

public function ConditionalFieldsFormHelper::processDependentFields in Conditional Fields 4.x

Same name and namespace in other branches
  1. 8 src/ConditionalFieldsFormHelper.php \Drupal\conditional_fields\ConditionalFieldsFormHelper::processDependentFields()

Build and attach #states properties to dependent fields.

File

src/ConditionalFieldsFormHelper.php, line 96

Class

ConditionalFieldsFormHelper
Helper to interact with forms.

Namespace

Drupal\conditional_fields

Code

public function processDependentFields() {
  $this->effects = [];

  // Cycle all dependents.
  foreach ($this->form['#conditional_fields'] as $dependent => $dependent_info) {
    $states = [];
    if (!isset($dependent_info['dependees']) || empty($dependent_info['dependees'])) {
      continue;
    }
    $dependees = $dependent_info['dependees'];
    $dependent_location = array_merge([], [
      $dependent,
    ]);
    $dependent_form_field = NestedArray::getValue($this->form, $dependent_location);
    $states = $this
      ->processDependeeFields($dependees, $dependent_form_field, $dependent_location, $states);
    if (empty($states)) {
      continue;
    }

    // Save the modified field back into the form.
    NestedArray::setValue($this->form, $dependent_location, $dependent_form_field);

    // Add the #states property to the dependent field.
    NestedArray::setValue($this->form, array_merge($dependent_location, [
      '#states',
    ]), $this
      ->mapStates($states));
  }
  return $this;
}