You are here

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

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

Determine and register dependee field effects.

1 call to ConditionalFieldsFormHelper::processDependeeFields()
ConditionalFieldsFormHelper::processDependentFields in src/ConditionalFieldsFormHelper.php
Build and attach #states properties to dependent fields.

File

src/ConditionalFieldsFormHelper.php, line 130

Class

ConditionalFieldsFormHelper
Helper to interact with forms.

Namespace

Drupal\conditional_fields

Code

public function processDependeeFields($dependees, &$dependent_form_field = [], $dependent_location = [], $states = []) {

  // Cycle the dependant's dependees.
  foreach ($dependees as $dependency) {
    $dependee = $dependency['dependee'];
    if (empty($this->form['#conditional_fields'][$dependee])) {
      continue;
    }
    $dependee_info = $this->form['#conditional_fields'][$dependee];
    end($dependee_info['parents']);
    $unset = key($dependee_info['parents']);
    if (is_int($dependee_info['parents'][$unset]) && $dependee_info['parents'][$unset] > 0) {
      unset($dependee_info['parents'][$unset]);
    }
    $dependee_form_field = NestedArray::getValue($this->form, $dependee_info['parents']);
    $options = $dependency['options'];

    // Apply reset dependent to default if untriggered behavior.
    if (!empty($options['reset'])) {

      // Add property to element so conditional_fields_dependent_validate()
      // can pick it up.
      $dependent_form_field['#conditional_fields_reset_if_untriggered'] = TRUE;
    }
    if (!empty($options['values']) && is_string($options['values'])) {
      $options['values'] = explode("\r\n", $options['values']);
    }
    $options['selector'] = $this
      ->getSelector($options['selector'], $dependee_form_field);
    $state = $this
      ->getState($dependee, $dependee_form_field, $options);

    // Add validation callback to element if the dependency can be evaluated.
    if (in_array($options['condition'], [
      'value',
      'empty',
      '!empty',
      'checked',
      '!checked',
    ])) {
      $dependent_form_field = $this
        ->elementAddProperty($dependent_form_field, '#element_validate', [
        self::class,
        'dependentValidate',
      ], 'append');
    }
    $states = $this
      ->addStateToGroup($state, $options, $states);
    $selector = $this
      ->buildJquerySelectorForField(NestedArray::getValue($this->form, [
      $dependent_location[0],
    ]));
    $this->effects[$selector] = $this
      ->getEffect($options);
  }
  return $states;
}