You are here

function addressfield_standard_widget_refresh in Address Field 7

Ajax callback in response to a change of country in an address field.

The only thing we have to do is to find the proper element to render.

2 string references to 'addressfield_standard_widget_refresh'
addressfield_format_address_ch_generate in example/plugins/format/address-ch-example.inc
addressfield_format_address_generate in plugins/format/address.inc
Format callback.

File

./addressfield.module, line 680
Defines a field for attaching country-specific addresses to entities.

Code

function addressfield_standard_widget_refresh($form, $form_state) {

  // The target element is one element below the triggering country selector.
  $array_parents = $form_state['triggering_element']['#array_parents'];
  array_pop($array_parents);

  // Iterate over the form parents to find the element.
  $element = $form;
  foreach ($array_parents as $name) {
    $element =& $element[$name];
    if (!empty($element['#addressfield'])) {
      break;
    }
  }

  // Return the address block, but remove the '_weight' element inserted
  // by the field API.
  unset($element['_weight']);

  // Replace the address field widget with the updated widget and focus on the
  // new country select list.
  $commands[] = ajax_command_replace(NULL, render($element));
  $commands[] = ajax_command_invoke('#' . $element['country']['#id'], 'focus');

  // Add the status messages inside the new addressfield's wrapper element,
  // just like core does.
  $commands[] = ajax_command_prepend(NULL, theme('status_messages'));

  // Allow other modules to add arbitrary AJAX commands on the refresh.
  drupal_alter('addressfield_standard_widget_refresh', $commands, $form, $form_state);
  return array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );
}