You are here

function commerce_addressbook_field_widget_form in Commerce Addressbook 7

Implements hook_field_widget_form().

File

./commerce_addressbook.module, line 111
:

Code

function commerce_addressbook_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  global $user;

  // @TODO: $uid shouldn't be gathered from the currently logged in user!
  $uid = $user->uid;
  switch ($instance['widget']['type']) {
    case 'commerce_addressbook_select':

      // Get a list of saved address entities, filtered on the current entity type & bundle
      // @TODO: test if this still works when the same commerce_addressbook_saved_profiles field
      // is attached to multiple bundles. Has currently only been tested with different fields per bundle.
      $options = commerce_addressbook_get_saved_addresses_options($uid, $instance['entity_type'], $instance['bundle']);
      $default = 0;

      // Don't show if no saved addresses are available.
      if (count($options) > 1) {

        // @TODO: remove hard coded customer reference
        $fieldname = 'commerce_customer_' . $instance['bundle'];

        // Try to get the currently selected address from the order first
        if (isset($form_state['order'])) {
          $order_wrapper = entity_metadata_wrapper('commerce_order', $form_state['order']);
          if ($val = $order_wrapper->{$fieldname}
            ->raw()) {
            $default = $val;
          }
        }
        elseif (isset($items[$delta]['saved_address_profiles'])) {
          $default = $items[$delta]['saved_address_profiles'];
        }
        $element['saved_address_profiles'] = array(
          // @TODO: better access control?
          '#access' => user_is_logged_in(),
          '#attributes' => array(
            'class' => array(
              'edit-commerce-addressbook-saved-address-profiles',
            ),
            'title' => '',
            'rel' => '',
          ),
          '#type' => 'select',
          '#title' => filter_xss($element['#title']),
          '#options' => $options,
          '#default_value' => $default,
          '#required' => FALSE,
          '#ajax' => array(
            'callback' => 'commerce_addressbook_address_form_refresh',
            // @TODO: make more generic
            'wrapper' => 'commerce-customer-profile-' . $instance['bundle'] . '-wrapper',
          ),
          '#element_validate' => array(
            'commerce_addressbook_saved_addresses_validate',
          ),
          '#prefix' => '<div class="commerce-addressbook-saved-address-profiles-field commerce-addressbook-saved-address-profiles-saved_address_profiles-field commerce-addressbook-saved-address-profiles-saved_address_profiles-field">',
          '#suffix' => '</div>',
          // Create the field name for the corresponding checkout pane.
          // TODO: This should actually be determined by the checkout pane's settings.
          '#order_fieldname' => $fieldname,
        );
      }
      else {

        // @TODO: what should we do here?
        $element['saved_address_profiles'] = array(
          '#type' => 'markup',
          '#value' => '',
        );
      }
      break;
  }
  return $element;
}