You are here

public function UcAddressesUcCountryFieldHandler::getFormField in Ubercart Addresses 7

Same name and namespace in other branches
  1. 6.2 handlers/ubercart.handlers.inc \UcAddressesUcCountryFieldHandler::getFormField()

Implements UcAddressesFieldHandler::getFormField().

Overrides UcAddressesFieldHandler::getFormField

File

handlers/ubercart.handlers.inc, line 268
Field handlers for Ubercart core address fields: first_name, last_name, company, etc.

Class

UcAddressesUcCountryFieldHandler
Class for the Ubercart country field.

Code

public function getFormField($form, $form_values) {
  $address = $this
    ->getAddress();
  $fieldName = $this
    ->getFieldName();
  $fieldValue = $address
    ->getField($fieldName);
  $default = isset($form_values[$fieldName]) ? $form_values[$fieldName] : $fieldValue;
  $result = db_select('uc_countries')
    ->condition('version', 0, '>')
    ->fields('uc_countries')
    ->orderBy('country_name', 'ASC')
    ->execute();
  $countries = array();
  foreach ($result as $country) {

    // Create option.
    $countries[$country->country_id] = t($country->country_name);

    // Save for later use.
    self::$countries[$country->country_id] = $country;
  }
  if (count($countries) == 0) {
    $countries[] = t('No countries found.');
  }
  else {

    // Sort countries list in natural order.
    natcasesort($countries);
  }
  if (empty($form['#key_prefix'])) {
    if ($address instanceof UcAddressesAddress) {

      // When no key prefix is set, use the address ID as part of the zone wrapper ID.
      $zone_wrapper_id = 'uc-address' . $address
        ->getId() . '-zone-wrapper';
    }
    else {

      // When no instance of UcAddressesAddress is given, we have no unique
      // value to create a wrapper for.
      $zone_wrapper_id = 'uc-address-zone-wrapper';
    }
  }
  else {

    // When a key prefix is set, make this part of the zone wrapper ID.
    $zone_wrapper_id = 'uc-store-address-' . str_replace('_', '-', $form['#key_prefix']) . '-zone-wrapper';
  }
  return array(
    $fieldName => array(
      '#type' => 'select',
      '#title' => $this
        ->getFieldTitle(),
      '#options' => $countries,
      '#required' => $this
        ->isFieldRequired(),
      '#default_value' => empty($default) ? uc_store_default_country() : $default,
      '#ajax' => array(
        'callback' => 'uc_store_update_address_field_zones',
        'wrapper' => $zone_wrapper_id,
        'progress' => array(
          'type' => 'throbber',
        ),
      ),
      '#element_validate' => array(
        'uc_addresses_validate_address_field_country',
      ),
      '#key_prefix' => empty($form['#key_prefix']) ? '' : $form['#key_prefix'],
    ),
  );
}