You are here

function ddf_update_callback in Dynamic dependent fields 7

Ajax callback returning ajax commands to update dependent widgets.

1 string reference to 'ddf_update_callback'
ddf_menu in ./ddf.module
Implements hook_menu().

File

./ddf.module, line 25

Code

function ddf_update_callback($controlling_field_name, $entity_type, $bundle, $entity_id) {
  $result = array(
    '#type' => 'ajax',
    '#commands' => array(),
  );
  $dependent_fields = db_select('ddf', 'd')
    ->fields('d', array(
    'dependent_field_name',
    'data',
  ))
    ->condition('field_name', $controlling_field_name)
    ->condition('entity_type', $entity_type)
    ->condition('bundle', $bundle)
    ->execute()
    ->fetchAllKeyed();
  if (empty($dependent_fields)) {
    return $result;
  }
  if ($entity_id == 'NULL') {
    $entity_id = NULL;
  }
  $parameters = drupal_get_query_parameters($_GET);
  $entity = NULL;
  if (!is_null($entity_id)) {
    $entity = entity_load_single($entity_type, $entity_id);
  }
  if (empty($entity)) {
    $entity = NULL;
  }

  // Replace '_none' => NULL for select widgets.
  foreach ($parameters as $field_name => $value) {
    if ($value === '_none') {
      $instance = field_info_instance($entity_type, $field_name, $bundle);
      if ($instance && $instance['widget']['type'] == 'options_select') {
        $parameters[$field_name] = NULL;
      }
    }
  }
  foreach ($dependent_fields as $dependent_field_name => $settings) {
    if (!empty($settings)) {
      $settings = unserialize($settings);
    }
    $dependent_field = field_info_field($dependent_field_name);
    if (is_null($dependent_field)) {
      continue;
    }
    $selector = '';
    if (isset($parameters['dep:' . $dependent_field_name])) {
      $selector = $parameters['dep:' . $dependent_field_name];
    }
    $commands = module_invoke_all('ddf_update_widget', $dependent_field, $parameters, $selector, $entity, $settings, $controlling_field_name, $entity_type, $bundle);
    if (empty($commands)) {
      continue;
    }
    $result['#commands'] = array_merge_recursive($result['#commands'], $commands);
  }
  return $result;
}