You are here

protected function ConditionalFieldsFormHelper::processDependentFields in Conditional Fields 8

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

Build and attach #states properties to dependent fields.

File

src/ConditionalFieldsFormHelper.php, line 62

Class

ConditionalFieldsFormHelper
Helper to interact with forms.

Namespace

Drupal\conditional_fields

Code

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

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

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

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