class UcAddressesDefaultAddressFieldHandler in Ubercart Addresses 6.2
Same name and namespace in other branches
- 7 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 - Implementation of hook_uc_addresses_fields().
- uc_addresses_uc_addresses_field_handlers in ./
uc_addresses.uc_addresses_fields.inc - Implementation of 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;
}
}