You are here

function conditional_fields_element_after_build in Conditional Fields 4.x

Same name and namespace in other branches
  1. 8 conditional_fields.module \conditional_fields_element_after_build()
  2. 7.3 conditional_fields.module \conditional_fields_element_after_build()

Processes form elements with dependencies.

Just adds a #conditional_fields property to the form with the needed data, which is used later in \Drupal\conditional_fields\ConditionalFieldsFormHelper::afterBuild():

  • The fields #parents property.
  • Field dependencies data.
1 string reference to 'conditional_fields_element_after_build'
conditional_fields_element_info_alter in ./conditional_fields.module
Implements hook_element_info_alter().

File

./conditional_fields.module, line 108
Contains conditional_fields.module.

Code

function conditional_fields_element_after_build($element, FormStateInterface &$form_state) {

  // A container with a field widget.
  // Element::children() is probably a better fit.
  if (isset($element['widget'])) {
    $field = $element['widget'];
  }
  else {
    $field = $element;
  }
  $first_parent = reset($field['#parents']);

  // No parents, so bail out.
  if (!isset($first_parent) || isset($field['#type']) && $field['#type'] == 'value') {
    return $element;
  }
  $form =& $form_state
    ->getCompleteForm();

  // Some fields do not have entity type and bundle properties.
  // In this case we try to use the properties from the form.
  // This is not an optimal solution, since in case of fields
  // in entities within entities they might not correspond,
  // and their dependencies will not be loaded.
  $build_info = $form_state
    ->getBuildInfo();
  if (method_exists($build_info['callback_object'], 'getEntity')) {
    $entity = $build_info['callback_object']
      ->getEntity();
    if ($entity instanceof EntityInterface) {
      $bundle = $entity
        ->bundle();
      $entity_type = $entity
        ->getEntityTypeId();

      /**
       * @deprecated Not actual from Drupal 8.7.0.
       * Media entity returns the actual bundle object, rather than id
       */
      if (is_object($bundle) && method_exists($bundle, 'getPluginId')) {
        $bundle = $bundle
          ->getPluginId();
      }
      $dependencies = conditional_fields_load_dependencies($entity_type, $bundle);
      if (!$dependencies) {
        return $element;
      }
      $field_name = reset($field['#array_parents']);

      // Attach dependent.
      if (isset($dependencies['dependents'][$field_name])) {
        foreach ($dependencies['dependents'][$field_name] as $id => $dependency) {
          if (!isset($form['#conditional_fields'][$field_name]['dependees'][$id]) || conditional_fields_is_priority_field($field)) {
            conditional_fields_attach_dependency($form, $form_state, [
              '#field_name' => $dependency['dependee'],
            ], $field, $dependency['options'], $id);
          }
        }
      }
      if (isset($dependencies['dependees'][$field_name])) {
        foreach ($dependencies['dependees'][$field_name] as $id => $dependency) {
          if (!isset($form['#conditional_fields'][$field_name]['dependents'][$id]) || conditional_fields_is_priority_field($field)) {
            conditional_fields_attach_dependency($form, $form_state, $field, [
              '#field_name' => $dependency['dependent'],
            ], $dependency['options'], $id);
          }
        }
      }
    }
  }
  return $element;
}