diff --git a/addressfield.module b/addressfield.module index 177d92b..243fc39 100644 --- a/addressfield.module +++ b/addressfield.module @@ -235,6 +235,26 @@ function _addressfield_render_address(&$format, $address) { } /** + * Helper function to get the field's properties' attributes array for use in + * format plugins. Returned array contains class and microdata attributes. + */ +function _addressfield_get_attributes($microdata = array()) { + $attributes = array(); + + foreach (array_keys(addressfield_data_property_info()) as $property) { + // If microdata is enabled, include the microdata attributes. + if (module_exists('microdata') && !empty($microdata[$property])) { + $attributes[$property] = $microdata[$property]['#attributes']; + } + + // Add the class. + $attributes[$property]['class'] = array(str_replace('_', '-', $property)); + } + + return $attributes; +} + +/** * @} End of "ingroup addressfield_format" */ @@ -306,6 +326,7 @@ function addressfield_field_info() { 'default_formatter' => 'addressfield_default', 'property_type' => 'addressfield', 'property_callbacks' => array('addressfield_property_info_callback'), + 'microdata' => TRUE, ); return $fields; @@ -604,6 +625,15 @@ function addressfield_field_formatter_view($entity_type, $entity, $field, $insta $settings = $display['settings']; $element = array(); + // If the microdata module is enabled, get the microdata attributes from the + // entity variable. Otherwise, initialize an empty array. + if (module_exists('microdata') && isset($entity->microdata[$field['field_name']])) { + $microdata = $entity->microdata[$field['field_name']]; + } + else { + $microdata = array(); + } + switch ($display['type']) { case 'addressfield_default': if (!empty($settings['use_widget_handlers'])) { @@ -620,6 +650,7 @@ function addressfield_field_formatter_view($entity_type, $entity, $field, $insta 'instance' => $instance, 'langcode' => $langcode, 'delta' => $delta, + 'microdata' => $microdata, ); $element[$delta] = addressfield_generate($address, $handlers, $context); } @@ -702,6 +733,7 @@ function addressfield_data_property_info($name = NULL) { 'type' => 'text', 'getter callback' => 'entity_property_verbatim_get', 'setter callback' => 'entity_property_verbatim_set', + 'microdata' => TRUE, ); } @@ -709,6 +741,56 @@ function addressfield_data_property_info($name = NULL) { } /** + * Implements hook_microdata_value_type_alter(). + */ +function addressfield_microdata_value_type_alter(&$types) { + $types['addressfield'] = 'item'; +} + + /** + * Implements hook_microdata_field_defaults. + * + * Sets default mappings for different vocabularies; for example, schema.org. + * This enables fields to provide mappings for different use cases. + */ +function addressfield_microdata_field_defaults() { + $field_settings = array(); + + // Add the vocabulary terms that are used to describe the data. Multiple + // schemes can be defined, allowing the user to choose which mapping to use. + // The array key is used to choose which mapping to use in the field instance + // settings form. + $field_settings['default_mappings']['schema.org'] = array( + '#itemprop' => array('address'), + '#itemtype' => 'http://schema.org/PostalAddress', + 'name_line' => array( + '#itemprop' => array('name'), + ), + 'organization_name' => array( + '#itemprop' => array('name'), + ), + 'premise' => array( + '#itemprop' => array('streetAddress'), + ), + 'locality' => array( + '#itemprop' => array('addressLocality'), + ), + 'administrative_area' => array( + '#itemprop' => array('addressRegion'), + ), + 'postal_code' => array( + '#itemprop' => array('postalCode'), + ), + 'country' => array( + '#itemprop' => array('addressCountry'), + ), + ); + + return $field_settings; + +} + +/** * Wraps country_get_list() for use as an Entity API options list. */ function _addressfield_country_options_list($field = NULL, $instance = NULL) { diff --git a/plugins/format/address.inc b/plugins/format/address.inc index d7745ab..02c068a 100644 --- a/plugins/format/address.inc +++ b/plugins/format/address.inc @@ -22,6 +22,16 @@ function addressfield_format_address_generate(&$format, $address, $context = arr // for international shipping. We extend it with country-specific heuristics // below. + // Get class and microdata attributes. If this is the form view, + // $context['microdata'] will not be set, so call without microdata to get + // the classes. + if (isset($context['microdata'])) { + $attributes = _addressfield_get_attributes($context['microdata']); + } + else { + $attributes = _addressfield_get_attributes(); + } + // The street block. $format['street_block'] = array( '#type' => 'addressfield_container', @@ -31,7 +41,7 @@ function addressfield_format_address_generate(&$format, $address, $context = arr $format['street_block']['thoroughfare'] = array( '#title' => t('Address 1'), '#tag' => 'div', - '#attributes' => array('class' => array('thoroughfare')), + '#attributes' => $attributes['thoroughfare'], '#size' => 30, // The #required will be automatically set to FALSE when processing. '#required' => TRUE, @@ -39,7 +49,7 @@ function addressfield_format_address_generate(&$format, $address, $context = arr $format['street_block']['premise'] = array( '#title' => t('Address 2'), '#tag' => 'div', - '#attributes' => array('class' => array('premise')), + '#attributes' => $attributes['premise'], '#size' => 30, ); $format['locality_block'] = array( @@ -52,20 +62,20 @@ function addressfield_format_address_generate(&$format, $address, $context = arr '#title' => t('Postal Code'), '#size' => 10, '#required' => TRUE, - '#attributes' => array('class' => array('postal-code')), + '#attributes' => $attributes['postal_code'], ); $format['locality_block']['locality'] = array( '#title' => t('City'), '#size' => 30, '#required' => TRUE, '#prefix' => ' ', - '#attributes' => array('class' => array('locality')), + '#attributes' => $attributes['locality'], ); $format['country'] = array( '#title' => t('Country'), '#options' => _addressfield_country_options_list(), '#required' => TRUE, - '#attributes' => array('class' => array('country')), + '#attributes' => $attributes['country'], '#weight' => 100, ); diff --git a/plugins/format/name-full.inc b/plugins/format/name-full.inc index 0dbe6a0..cf9b16d 100644 --- a/plugins/format/name-full.inc +++ b/plugins/format/name-full.inc @@ -17,7 +17,17 @@ $plugin = array( * * @see CALLBACK_addressfield_format_callback() */ -function addressfield_format_name_full_generate(&$format, $address) { +function addressfield_format_name_full_generate(&$format, $address, $context = array()) { + // Get class and microdata attributes. If this is the form view, + // $context['microdata'] will not be set, so call without microdata to get + // the classes. + if (isset($context['microdata'])) { + $attributes = _addressfield_get_attributes($context['microdata']); + } + else { + $attributes = _addressfield_get_attributes(); + } + $format['name_block'] = array( '#type' => 'addressfield_container', '#attributes' => array('class' => array('addressfield-container-inline', 'name-block')), @@ -27,13 +37,13 @@ function addressfield_format_name_full_generate(&$format, $address) { '#title' => t('First name'), '#size' => 30, '#required' => TRUE, - '#attributes' => array('class' => array('first-name')), + '#attributes' => $attributes['first_name'], ); $format['name_block']['last_name'] = array( '#title' => t('Last name'), '#size' => 30, '#required' => TRUE, '#prefix' => ' ', - '#attributes' => array('class' => array('last-name')), + '#attributes' => $attributes['last_name'], ); } diff --git a/plugins/format/name-oneline.inc b/plugins/format/name-oneline.inc index c1c5a29..88b4380 100644 --- a/plugins/format/name-oneline.inc +++ b/plugins/format/name-oneline.inc @@ -17,7 +17,17 @@ $plugin = array( * * @see CALLBACK_addressfield_format_callback() */ -function addressfield_format_name_oneline_generate(&$format, $address) { +function addressfield_format_name_oneline_generate(&$format, $address, $context = array()) { + // Get class and microdata attributes. If this is the form view, + // $context['microdata'] will not be set, so call without microdata to get + // the classes. + if (isset($context['microdata'])) { + $attributes = _addressfield_get_attributes($context['microdata']); + } + else { + $attributes = _addressfield_get_attributes(); + } + $format['name_block'] = array( '#type' => 'addressfield_container', '#attributes' => array('class' => array('addressfield-container-inline', 'name-block')), @@ -26,7 +36,7 @@ function addressfield_format_name_oneline_generate(&$format, $address) { $format['name_block']['name_line'] = array( '#title' => t('Full name'), '#tag' => 'div', - '#attributes' => array('class' => array('name-block')), + '#attributes' => $attributes['name_line'], '#size' => 30, '#required' => TRUE, ); diff --git a/plugins/format/organisation.inc b/plugins/format/organisation.inc index c069a4c..1e53fbd 100644 --- a/plugins/format/organisation.inc +++ b/plugins/format/organisation.inc @@ -17,7 +17,17 @@ $plugin = array( * * @see CALLBACK_addressfield_format_callback() */ -function addressfield_format_organisation_generate(&$format, $address) { +function addressfield_format_organisation_generate(&$format, $address, $context = array()) { + // Get class and microdata attributes. If this is the form view, + // $context['microdata'] will not be set, so call without microdata to get + // the classes. + if (isset($context['microdata'])) { + $attributes = _addressfield_get_attributes($context['microdata']); + } + else { + $attributes = _addressfield_get_attributes(); + } + $format['organisation_block'] = array( '#type' => 'addressfield_container', '#attributes' => array('class' => array('addressfield-container-inline', 'name-block')), @@ -26,6 +36,6 @@ function addressfield_format_organisation_generate(&$format, $address) { $format['organisation_block']['organisation_name'] = array( '#title' => t('Company'), '#size' => 30, - '#attributes' => array('class' => array('organisation-name')), + '#attributes' => $attributes['organisation_name'], ); }