You are here

function ddf_element_after_build in Dynamic dependent fields 7

1 string reference to 'ddf_element_after_build'
ddf_element_info_alter in ./ddf.module
Implements hook_element_info_alter().

File

./ddf.module, line 233

Code

function ddf_element_after_build($element, &$form_state) {

  // Ensure that the element is a field.
  if (isset($element['#field_name'])) {
    $field =& $element;
  }
  elseif (isset($element['#language'], $element[$element['#language']], $element[$element['#language']]['#field_name'])) {

    // Some fields are wrapped in containers before processing.
    $field =& $element[$element['#language']];
  }
  else {
    return $element;
  }

  // Do not process hidden fields.
  if (isset($field['#access']) && $field['#access'] == FALSE) {
    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;
  }
  $entity_type = NULL;
  $bundle = NULL;
  $entity = NULL;
  $entity_id = NULL;
  if (isset($field['#entity_type'], $field['#bundle'])) {
    $entity_type = $field['#entity_type'];
    $bundle = $field['#bundle'];
    if (isset($field['#entity'])) {
      $entity = $field['#entity'];
    }
  }
  elseif (isset($form['#entity_type'], $form['#bundle'])) {
    $entity_type = $form['#entity_type'];
    $bundle = $form['#bundle'];
    if (isset($form['#entity'])) {
      $entity = $form['#entity'];
    }
  }
  else {
    return $element;
  }
  $dependencies = ddf_load_dependencies($entity_type, $bundle);
  if (empty($dependencies)) {
    return $element;
  }
  if (!empty($entity)) {
    list($entity_id, , ) = entity_extract_ids($entity_type, $entity);
  }
  if (empty($entity_id)) {
    $entity_id = 'NULL';
  }
  foreach ($dependencies as $dependency) {

    // Process dependent field.
    if ($dependency[1] == $field['#field_name']) {
      if (isset($field['#name'])) {
        $parents = '';
        if (!empty($field['#field_parents'])) {
          $parents = ':' . implode(':', $field['#field_parents']);
        }
        $settings = array(
          'ddf' => array(
            $form['#build_id'] . $parents . ':' . $entity_type . ':' . $bundle => array(
              'form_id' => $form['#build_id'],
              'dependent' => array(
                $field['#field_name'] => $field['#name'],
              ),
            ),
          ),
        );
        drupal_add_js($settings, 'setting');
      }
    }

    // Process controlling field.
    if ($dependency[0] == $field['#field_name']) {
      if (isset($field['#name'])) {
        $parents = '';
        if (!empty($field['#field_parents'])) {
          $parents = ':' . implode(':', $field['#field_parents']);
        }
        $settings = array(
          'ddf' => array(
            $form['#build_id'] . $parents . ':' . $entity_type . ':' . $bundle => array(
              'form_id' => $form['#build_id'],
              'entity_type' => $entity_type,
              'bundle' => $bundle,
              'entity_id' => $entity_id,
              'fields' => array(
                $field['#field_name'] => $field['#name'],
              ),
            ),
          ),
        );
        drupal_add_js($settings, 'setting');
        drupal_add_library('system', 'drupal.ajax');
        drupal_add_library('system', 'jquery.form');
        drupal_add_js(drupal_get_path('module', 'ddf') . '/ddf.js');
      }
    }
  }
  return $element;
}