function conditional_fields_element_after_build in Conditional Fields 7.3
Same name and namespace in other branches
- 8 conditional_fields.module \conditional_fields_element_after_build()
- 4.x 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 conditional_fields_form_after_build():
- 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(). Adds an #after_build function to all form elements.
File
- ./
conditional_fields.module, line 221 - Define dependencies between fields based on their states and values.
Code
function conditional_fields_element_after_build($element, &$form_state) {
// Some fields are wrapped in containers before processing. Wrapped data must
// take precedence over the container, because Entity Translation and
// possibly other modules add #field_name to the container as well.
if (isset($element['#language'], $element[$element['#language']], $element[$element['#language']]['#field_name'])) {
$field = $element[$element['#language']];
}
elseif (isset($element['#field_name'])) {
$field = $element;
}
else {
return $element;
}
$form =& $form_state['complete form'];
// Avoid processing fields in fields_ui administration pages.
if (drupal_substr($form['#form_id'], 0, 9) == 'field_ui_') {
return $element;
}
// 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.
if (isset($field['#entity_type'], $field['#bundle'])) {
$entity_type = $field['#entity_type'];
$bundle = $field['#bundle'];
}
elseif (isset($form['#entity_type'], $form['#bundle'])) {
$entity_type = $form['#entity_type'];
$bundle = $form['#bundle'];
}
else {
return $element;
}
$dependencies = conditional_fields_load_dependencies($entity_type, $bundle);
if (!$dependencies) {
return $element;
}
// Attach dependent.
if (isset($dependencies['dependents'][$field['#field_name']])) {
foreach ($dependencies['dependents'][$field['#field_name']] as $id => $dependency) {
if (!isset($form['#conditional_fields'][$field['#field_name']]['dependees'][$id])) {
conditional_fields_attach_dependency($form, array(
'#field_name' => $dependency['dependee'],
), $field, $dependency['options'], $id);
}
}
}
// Attach dependee.
// TODO: collect information about every element of the dependee widget, not
// just the first encountered. This bottom-up approach would allow us to
// define per-element sets of dependency values.
if (isset($dependencies['dependees'][$field['#field_name']])) {
foreach ($dependencies['dependees'][$field['#field_name']] as $id => $dependency) {
if (!isset($form['#conditional_fields'][$field['#field_name']]['dependents'][$id])) {
conditional_fields_attach_dependency($form, $field, array(
'#field_name' => $dependency['dependent'],
), $dependency['options'], $id);
}
}
}
return $element;
}