You are here

function uc_addresses_get_address_form in Ubercart Addresses 7

Same name and namespace in other branches
  1. 5.2 uc_addresses.module \uc_addresses_get_address_form()
  2. 5 uc_addresses.module \uc_addresses_get_address_form()
  3. 6.2 uc_addresses.pages.inc \uc_addresses_get_address_form()
  4. 6 uc_addresses.module \uc_addresses_get_address_form()

Creates a form used to add a new address or edit an existing address.

Parameters

array $form: The definition of the form.

array $form_state: The form state.

object $address_user: The user who "owns" this address.

UcAddressesAddress $address: The address to edit (NULL for new addresses).

Return value

array An address form.

1 string reference to 'uc_addresses_get_address_form'
uc_addresses_menu in ./uc_addresses.module
Implements hook_menu().

File

./uc_addresses.pages.inc, line 318
Page callbacks for viewing, adding, editing, and deleting addresses.

Code

function uc_addresses_get_address_form($form, &$form_state, $address_user, $address = NULL) {
  if (!$address instanceof UcAddressesAddress) {

    // Try to retrieve from form_state first.
    if (isset($form_state['input']['address']['aid'])) {
      $address = UcAddressesAddressBook::get($address_user)
        ->getAddressById($form_state['input']['address']['aid']);
    }
  }

  // If we have still no valid address instance, create a new instance.
  if (!$address instanceof UcAddressesAddress) {
    $address = UcAddressesAddressBook::get($address_user)
      ->addAddress();
  }
  $form['uc_addresses'] = array(
    '#type' => 'fieldset',
    '#title' => $address
      ->isNew() ? t('Add an address') : t('Edit address information'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['uc_addresses']['address'] = array(
    '#type' => 'uc_addresses_address',
    '#uc_addresses_address' => $address,
    '#uc_addresses_context' => 'address_form',
  );

  // Add submit button.
  if ($address
    ->isNew()) {

    // New address.
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => variable_get('uc_addresses_add_button', t('Save address')),
    );
  }
  else {

    // Existing address.
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => variable_get('uc_addresses_update_button', t('Save address')),
    );
    $form['delete'] = array(
      '#type' => 'submit',
      '#value' => variable_get('uc_addresses_delete_button', t('Delete address')),
      '#limit_validation_errors' => array(),
      '#submit' => array(
        'uc_addresses_get_address_form_submit',
      ),
    );

    // Check if address may be deleted.
    // (It's not allowed to delete default addresses).
    if (!UcAddressesPermissions::canDeleteAddress($address_user, $address)) {
      $form['delete']['#access'] = FALSE;
    }
  }

  // Add cancel link.
  $form['cancel'] = array(
    '#markup' => l(t('Cancel'), 'user/' . $address_user->uid . '/addresses/', array(
      'attributes' => array(
        'class' => 'address-link cancel-address-link',
      ),
    )),
  );
  return $form;
}