The uc_addresses_address_select form element

Last updated on
9 May 2017

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

This form element is based on the "select" form element and is used to display a list of selectable addresses. If setup properly, it will automatically fill in the form element of type "uc_addresses_address" using ajax.

This form element exists in the Drupal 7 version of Ubercart Addresses only.

Default values

This are the default values a form element of type "uc_addresses_address_select" will get if not set.

Properties

#uc_addresses_select_addresses

Description: An array of UcAddressesAddress instances that can be selected in the form.
Usage example:

// Example: let the user choose addresses from it's own address book.
global $user;
$addresses = UcAddressesAddressBook::get($user->uid)->getAddresses();
if (count($addresses) > 0) {
  // Create form element only when the user has addresses.
  $form['select_address'] = array(
    '#type' => 'uc_addresses_address_select',
    '#title' => t('Address book'),
    '#uc_addresses_select_addresses' => $addresses,
  );
}

#uc_addresses_address_element

Description: Array. Specifies which address element needs to be updated upon selecting an address and where that element is located in the form. For example, if your address element is called "address" and you have put it in a fieldset called "billing" and that element is located at the root of the form, then its location is: array('billing', 'address') Usage example: see further.

#uc_addresses_address_input

Description: Array. Specifies where in the form's state "input" array the address values will be when the form is submitted. The module uses this value to overwrite the form's state "input" values to prevent any "Illegal choice" errors for the zone field when you select a different address.
This value can be equal to #uc_addresses_address_element, but that don't have to be. If the address element is nested deep in the form array, but not every level in this array has #tree to TRUE, it will be different. For example, if you nest the address element like this: form root - panes - shipping - address, and "panes" has '#tree' = FALSE (or not set), but shipping '#tree' = TRUE, the form's state input array will look something like this when the form is submitted:

$form_state['input'] = array(
  'shipping' => array(
    'address' => array(
      'first_name' => '',
      'last_name' => '',
      // (other address fields)
    ),
  ),
  'op' => 'submit',
  'form_build_id' => 'form-opBWusv32GC6uFAQsjD4KjXY7rBT10rnOg0DGrdtqfo',
  'form_token' => 'QJr0OjV9vMAOfOfIFj8EnevKpGo959pxL19r1qB_N2M',
  'form_id' => 'uc_addresses_test_address_form',
);

This means the address values can be found in form state - input - shipping - address (and NOT form state - input - panes - shipping - address). You'll have to give up array('shipping', 'address') in this case. If "panes" would have set '#tree' = TRUE, it would have been array('panes', 'shipping', 'address') as then the form state structure would look like this when the form is submitted:

$form_state['input'] = array(
  'panes' => array(
    'shipping' => array(
      'address' => array(
        'first_name' => '',
        'last_name' => '',
        // (other address fields)
      ),
    ),
  ),
  'op' => 'submit',
  'form_build_id' => 'form-opBWusv32GC6uFAQsjD4KjXY7rBT10rnOg0DGrdtqfo',
  'form_token' => 'QJr0OjV9vMAOfOfIFj8EnevKpGo959pxL19r1qB_N2M',
  'form_id' => 'uc_addresses_test_address_form',
);

Usage example: see further.

Usage example

A complete example of the usage of the uc_addresses_address_select element:

global $user;
$addresses = UcAddressesAddressBook::get($user->uid)->getAddresses();
if (count($addresses) > 0) {
  // Create form element only when the user has addresses.
  $form['select_address'] = array(
    '#type' => 'uc_addresses_address_select',
    '#title' => t('Address book'),
    '#uc_addresses_select_addresses' => $addresses,
    '#uc_addresses_address_element' => array('billing', 'address'), // The "location" of the address element in the form.
    '#uc_addresses_address_input' => array('address'), // The "location" of the address values in the form's state "input".
    '#ajax' => array(
      'callback' => 'uc_addresses_address_render',
      'wrapper' => 'billing-address-pane', // Tells which part of the page to update via ajax.
      'progress' => array(
        'type' => 'throbber',
      ),
      'event' => 'change',
    ),
  );
}
$form['billing']['address'] = array(
  '#type' => 'uc_addresses_address',
  '#uc_addresses_required' => FALSE,
  '#uc_addresses_context' => 'address_form',    
  '#prefix' => '<div id="billing-address-pane">', // Important for updating the form via ajax.
  '#suffix' => '</div>',
);

Help improve this page

Page status: No known problems

You can: