diff --git a/tests/uc_addresses.entity.test b/tests/uc_addresses.entity.test index 22dfc74..59ce03d 100644 --- a/tests/uc_addresses.entity.test +++ b/tests/uc_addresses.entity.test @@ -400,10 +400,21 @@ class UcAddressesEntityCase extends UcAddressesTestCase { $wrapper = entity_metadata_wrapper('uc_addresses', $entity); foreach ($values as $field => $value) { + switch ($field) { + case 'uid': + // The entity property for the 'uid' field is called 'user'. + $property = 'user'; + break; + default: + // All other entity property names are equal to the field name. + $property = $field; + break; + } + $msgvars = array( '@field' => check_plain($field), ); - $this->assertEqual($wrapper->$field->value(), $value, strtr('Entity wrapper returns expected value for @field.', $msgvars)); + $this->assertEqual($wrapper->$property->raw(), $value, strtr('Entity wrapper returns expected value for @field.', $msgvars)); } } diff --git a/uc_addresses.api.php b/uc_addresses.api.php index 17b3998..af08673 100644 --- a/uc_addresses.api.php +++ b/uc_addresses.api.php @@ -49,6 +49,7 @@ function hook_uc_addresses_field_handlers() { * @return array * An associative array containing: * - title: the title of the field, safe for output. + * - type: (optional) The data type of the property. * - handler: handler class, registered through hook_uc_addresses_field_handlers(). * - display_settings: (optional) An array of contexts to show or hide the * field on: @@ -67,19 +68,31 @@ function hook_uc_addresses_field_handlers() { * comparisons? * An address comparison is done to avoid having double addresses in the * address book. + * - (additional data used by Entity API, see hook_entity_property_info().) * * Optionally you can define extra properties in the definition. Properties can be * reached from within the handler by calling getProperty(). When a handler uses extra * properties, these properties will be required. Check the documentation of the handler * to see which extra properties it requires. * + * Since each field is also a property of the uc_addresses entity, the definition + * for each field will also be passed to Entity API as metadata. This means that + * implementing hook_entity_property_info() separately to define additional (or + * similar) information about your field is not needed. Your field will automatically + * become available in Rules, for example. + * However, you may need to define extra properties for your field in order to let + * it operate properly in entity related functionalities. Check the documentation of + * hook_entity_property_info() to see which extra properties you can define. + * * @see hook_uc_addresses_field_handlers() + * @see hook_entity_property_info() */ function hook_uc_addresses_fields() { // Example: register my own field return array( 'myfield' => array( 'title' => t('My field'), + 'type' => 'text', 'handler' => 'MyCustomFieldHandler', 'display_settings' => array( 'default' => TRUE, // Display it by default diff --git a/uc_addresses.module b/uc_addresses.module index c028d36..c596e44 100644 --- a/uc_addresses.module +++ b/uc_addresses.module @@ -452,13 +452,18 @@ function uc_addresses_entity_property_info_alter(&$info) { } else { $properties[$key] = $field; - $properties[$key]['label'] = $properties[$key]['title']; } $properties[$key]['getter callback'] = 'uc_addresses_field_get'; if ($key != 'aid' || $key != 'uid') { $properties[$key]['setter callback'] = 'uc_addresses_field_set'; } } + + // Special case for user, this should be mapped to 'user' instead + // of 'uid' as noted on http://drupal.org/node/1021466. + $properties['user'] = $properties['uid']; + $properties['user']['label'] = t('User'); + unset($properties['uid']); } /** @@ -1125,28 +1130,23 @@ function uc_addresses_get_address_fields() { foreach ($fields as $fieldname => $fielddata) { // Add name for the field (never set manually). $fields[$fieldname]['name'] = $fieldname; - // Add title if not set. - if (!isset($fielddata['title'])) { - $fields[$fieldname]['title'] = check_plain($fieldname); - } - // Add description if not set. - if (!isset($fielddata['description'])) { - $fields[$fieldname]['description'] = ''; - } - // Add display settings array - if (!isset($fielddata['display_settings'])) { - $fields[$fieldname]['display_settings'] = array(); - } - if (!isset($fielddata['display_settings']['default'])) { - $fields[$fieldname]['display_settings']['default'] = TRUE; - } - // Add compare setting - if (!isset($fielddata['compare'])) { - $fields[$fieldname]['compare'] = TRUE; - } - // Add hidden setting (only applies for address field settings page) - if (!isset($fielddata['hidden'])) { - $fields[$fieldname]['hidden'] = FALSE; + // Add default values for properties that are not set. + $fields[$fieldname] += array( + 'type' => 'text', + 'title' => check_plain($fieldname), + 'description' => '', + 'display_settings' => array(), + 'compare' => TRUE, + // The hidden setting only applies for the address field settings page. + 'hidden' => FALSE, + ); + // Add default display settings. + $fields[$fieldname]['display_settings'] += array( + 'default' => TRUE, + ); + // Add label if not set. Used by Entity API. + if (!isset($fields[$fieldname]['label'])) { + $fields[$fieldname]['label'] = $fields[$fieldname]['title']; } } } diff --git a/uc_addresses.uc_addresses_fields.inc b/uc_addresses.uc_addresses_fields.inc index 4017f38..c386c36 100644 --- a/uc_addresses.uc_addresses_fields.inc +++ b/uc_addresses.uc_addresses_fields.inc @@ -142,50 +142,72 @@ function uc_addresses_uc_addresses_fields() { // Ubercart Core Fields $data['first_name'] = array( + 'type' => 'text', + 'label' => t('First name'), 'description' => t('The addressee\'s first name'), 'handler' => 'UcAddressesUcTextFieldHandler', 'display_settings' => $ubercart_display_settings, ); $data['last_name'] = array( + 'type' => 'text', + 'label' => t('Last name'), 'description' => t('The addressee\'s last name'), 'handler' => 'UcAddressesUcTextFieldHandler', 'display_settings' => $ubercart_display_settings, ); $data['phone'] = array( + 'type' => 'text', + 'label' => t('Phone'), 'description' => t('The addressee\'s phone number'), 'handler' => 'UcAddressesUcTextFieldHandler', ); $data['company'] = array( + 'type' => 'text', + 'label' => t('Company'), 'description' => t('The addressee\'s company name'), 'handler' => 'UcAddressesUcTextFieldHandler', 'display_settings' => $ubercart_display_settings, ); $data['street1'] = array( + 'type' => 'text', + 'label' => t('Street line 1'), 'description' => t('The addressee\'s residence number and street'), 'handler' => 'UcAddressesUcTextFieldHandler', 'display_settings' => $ubercart_display_settings, ); $data['street2'] = array( + 'type' => 'text', + 'label' => t('Street line 2'), 'description' => t('The addressee\'s residence number and street (continued)'), 'handler' => 'UcAddressesUcTextFieldHandler', 'display_settings' => $ubercart_display_settings, ); $data['city'] = array( + 'type' => 'text', + 'label' => t('City'), 'description' => t('The addressee\'s city of residence'), 'handler' => 'UcAddressesUcTextFieldHandler', 'display_settings' => $ubercart_display_settings, ); $data['country'] = array( + 'type' => 'integer', + 'label' => t('Country'), 'description' => t('The addressee\'s country of residence'), 'handler' => 'UcAddressesUcCountryFieldHandler', 'display_settings' => $ubercart_display_settings, + 'options list' => 'uc_country_option_list', ); $data['zone'] = array( + 'type' => 'integer', + 'label' => t('Zone'), 'description' => t('The addressee\'s zone of residence'), 'handler' => 'UcAddressesUcZoneFieldHandler', 'display_settings' => $ubercart_display_settings, + 'options list' => 'uc_zone_option_list', ); $data['postal_code'] = array( + 'type' => 'text', + 'label' => t('Postal code'), 'description' => t('The addressee\'s postal code'), 'handler' => 'UcAddressesUcTextFieldHandler', 'display_settings' => $ubercart_display_settings, @@ -194,6 +216,7 @@ function uc_addresses_uc_addresses_fields() { // Ubercart Addresses fields $data['address_name'] = array( 'title' => t('Address name'), + 'type' => 'text', 'description' => t('The name used to access this address'), 'handler' => 'UcAddressesAddressNameFieldHandler', 'display_settings' => array( @@ -207,6 +230,7 @@ function uc_addresses_uc_addresses_fields() { ); $data['default_shipping'] = array( 'title' => t('Default shipping address'), + 'type' => 'boolean', 'description' => t('If the address is the default shipping address'), 'handler' => 'UcAddressesDefaultAddressFieldHandler', 'display_settings' => array( @@ -223,6 +247,7 @@ function uc_addresses_uc_addresses_fields() { ); $data['default_billing'] = array( 'title' => t('Default billing address'), + 'type' => 'boolean', 'description' => t('If the address is the default billing address'), 'handler' => 'UcAddressesDefaultAddressFieldHandler', 'display_settings' => array( @@ -240,6 +265,7 @@ function uc_addresses_uc_addresses_fields() { // Non editable fields $data['aid'] = array( 'title' => t('Address ID'), + 'type' => 'integer', 'description' => t('The address ID'), 'handler' => 'UcAddressesHiddenFieldHandler', 'display_settings' => array( @@ -250,6 +276,7 @@ function uc_addresses_uc_addresses_fields() { ); $data['uid'] = array( 'title' => t('User ID'), + 'type' => 'user', 'description' => t('The ID of the user who owns this address'), 'handler' => 'UcAddressesHiddenFieldHandler', 'display_settings' => array( @@ -260,6 +287,7 @@ function uc_addresses_uc_addresses_fields() { ); $data['created'] = array( 'title' => t('Created'), + 'type' => 'date', 'description' => t('The date this address was created'), 'handler' => 'UcAddressesDateFieldHandler', 'display_settings' => array( @@ -270,6 +298,7 @@ function uc_addresses_uc_addresses_fields() { ); $data['modified'] = array( 'title' => t('Modified'), + 'type' => 'date', 'description' => t('The date this address was last modified'), 'handler' => 'UcAddressesDateFieldHandler', 'display_settings' => array(