You are here

class UcAddressesDefaultAddressFieldHandler in Ubercart Addresses 7

Same name and namespace in other branches
  1. 6.2 handlers/uc_addresses.handlers.inc \UcAddressesDefaultAddressFieldHandler

Class for default address field.

This class requires extra properties in the field definition:

  • default_type: lowercase string that specifies the kind of default address (e.g., shipping);
  • suffix: used as a suffix to the form field in address edit forms.

Hierarchy

Expanded class hierarchy of UcAddressesDefaultAddressFieldHandler

2 string references to 'UcAddressesDefaultAddressFieldHandler'
uc_addresses_uc_addresses_fields in ./uc_addresses.uc_addresses_fields.inc
Implements hook_uc_addresses_fields().
uc_addresses_uc_addresses_field_handlers in ./uc_addresses.uc_addresses_fields.inc
Implements hook_uc_addresses_field_handlers().

File

handlers/uc_addresses.handlers.inc, line 130
Field handlers for Ubercart Addresses address fields:

View source
class UcAddressesDefaultAddressFieldHandler extends UcAddressesFieldHandler {

  /**
   * Implements UcAddressesFieldHandler::init().
   *
   * @throws UcAddressesIncompatibleHandlerException
   *   In case the given address instance is not an instance of
   *   UcAddressesAddress.
   */
  protected function init() {
    $address = $this
      ->getAddress();
    if (!$address instanceof UcAddressesAddress) {
      throw new UcAddressesIncompatibleHandlerException(t('The handler %handler needs an UcAddressesAddress instance to function.', array(
        '%handler' => get_class($this),
      )));
    }
  }

  /**
   * Implements UcAddressesFieldHandler::getFormField().
   */
  public function getFormField($form, $form_values) {
    $fieldName = $this
      ->getFieldName();
    $fieldValue = $this
      ->getAddress()
      ->getField($fieldName);
    $default = isset($form_values[$fieldName]) ? $form_values[$fieldName] : $fieldValue;
    $default_type = $this
      ->getProperty('default_type');

    // Check if user already has a default address of type $type.
    $default_address = $this
      ->getAddress()
      ->getAddressBook()
      ->getDefaultAddress($default_type);
    if (!is_null($default_address)) {
      return array(
        $fieldName => array(
          '#type' => 'checkbox',
          '#title' => $this
            ->getFieldTitle(),
          '#uc_addresses_default_address_suffix' => $this
            ->getProperty('suffix'),
          '#default_value' => $default,
          '#disabled' => $fieldValue,
          '#required' => $this
            ->isFieldRequired(),
          '#theme' => 'uc_addresses_default_address_checkbox',
        ),
      );
    }
    else {

      // No default address of type $type yet. Force default.
      return array(
        $fieldName => array(
          '#type' => 'value',
          '#value' => 1,
        ),
        $fieldName . '_item' => array(
          '#type' => 'checkbox',
          '#title' => $this
            ->getFieldTitle(),
          '#uc_addresses_default_address_suffix' => $this
            ->getProperty('suffix'),
          '#default_value' => 1,
          '#disabled' => TRUE,
          '#required' => $this
            ->isFieldRequired(),
          '#theme' => 'uc_addresses_default_address_checkbox',
        ),
      );
    }
  }

  /**
   * Implements UcAddressesFieldHandler::isFieldEnabled().
   */
  public function isFieldEnabled() {
    return variable_get('uc_addresses_use_default_' . $this
      ->getProperty('default_type'), TRUE);
  }

  /**
   * Implements UcAddressesFieldHandler::isFieldRequired().
   */
  public function isFieldRequired() {
    return FALSE;
  }

  /**
   * Overrides UcAddressesFieldHandler::getDefaultValue().
   */
  public function getDefaultValue() {
    return 0;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UcAddressesDefaultAddressFieldHandler::getDefaultValue public function Overrides UcAddressesFieldHandler::getDefaultValue(). Overrides UcAddressesFieldHandler::getDefaultValue
UcAddressesDefaultAddressFieldHandler::getFormField public function Implements UcAddressesFieldHandler::getFormField(). Overrides UcAddressesFieldHandler::getFormField
UcAddressesDefaultAddressFieldHandler::init protected function Implements UcAddressesFieldHandler::init(). Overrides UcAddressesFieldHandler::init
UcAddressesDefaultAddressFieldHandler::isFieldEnabled public function Implements UcAddressesFieldHandler::isFieldEnabled(). Overrides UcAddressesFieldHandler::isFieldEnabled
UcAddressesDefaultAddressFieldHandler::isFieldRequired public function Implements UcAddressesFieldHandler::isFieldRequired(). Overrides UcAddressesFieldHandler::isFieldRequired
UcAddressesFieldHandler::$address private property Address object.
UcAddressesFieldHandler::$context private property The context in which this field is used.
UcAddressesFieldHandler::$definition private property The declared field definition.
UcAddressesFieldHandler::$name private property Name of this field.
UcAddressesFieldHandler::checkContext public function Checks if the field passes the context. 1
UcAddressesFieldHandler::getAddress final public function Returns the address attached to this field.
UcAddressesFieldHandler::getContext final public function Returns the context in which this field is used.
UcAddressesFieldHandler::getFieldName final public function Returns the field name.
UcAddressesFieldHandler::getFieldTitle public function Returns the title to use when displaying a field. 1
UcAddressesFieldHandler::getMappingTargets public function Returns supported mapping targets for Feeds. 2
UcAddressesFieldHandler::getOutputFormats public function Returns an array of possible output formats the handler supports. 2
UcAddressesFieldHandler::getProperty final public function Returns a property from the field definition.
UcAddressesFieldHandler::getTokenInfo public function Returns supported tokens. 1
UcAddressesFieldHandler::mapValue public function Set a fields value based on the output format. 2
UcAddressesFieldHandler::outputValue public function Output a field's value. 3
UcAddressesFieldHandler::setValue public function Sets value in the address object.
UcAddressesFieldHandler::validateValue public function Check a fields' value. 2
UcAddressesFieldHandler::__construct final public function UcAddressesFormField object constructor.