diff --git a/addressfield.module b/addressfield.module index 30beb1a..1e3f9e7 100644 --- a/addressfield.module +++ b/addressfield.module @@ -13,7 +13,7 @@ function addressfield_theme() { $path = drupal_get_path('module', 'addressfield') . '/theme'; $templates['addressfield_formatter'] = array( - 'variables' => array('address' => NULL), + 'variables' => array('address' => NULL, 'microdata' => NULL), 'template' => 'addressfield-formatter', 'path' => $path, ); @@ -37,6 +37,9 @@ function addressfield_field_info() { 'default_widget' => 'addressfield_standard', 'default_formatter' => 'addressfield_default', 'property_type' => 'addressfield', + // The microdata type informs the microdata module what options to display + // in the UI and how to place the itemprop attribute for this field. + 'microdata_type' => 'item', 'property_callbacks' => array('addressfield_property_info_callback'), ); @@ -167,6 +170,50 @@ function addressfield_field_schema() { } /** + * 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 microdata admin + // form. This array can override the default value_types by setting a + // 'value_type' on the array element. + $field_settings['default_mappings']['schema.org'] = array( + '#itemprops' => array('http://schema.org/address'), + '#itemtype' => 'http://schema.org/PostalAddress', + 'name_line' => array( + '#itemprops' => array('name'), + ), + 'organization_name' => array( + '#itemprops' => array('name'), + ), + 'premise' => array( + '#itemprops' => array('streetAddress'), + ), + 'locality' => array( + '#itemprops' => array('addressLocality'), + ), + 'administrative_area' => array( + '#itemprops' => array('addressRegion'), + ), + 'postal_code' => array( + '#itemprops' => array('postalCode'), + ), + 'country' => array( + '#itemprops' => array('addressCountry'), + ), + ); + + return $field_settings; + +} + +/** * Returns an array of default values for the addressfield form elements. */ function addressfield_default_values() { @@ -642,12 +689,27 @@ 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, set them to empty strings so we don't have to + // check later on. + if (module_exists('microdata') && isset($entity->{$field['field_name']}['#microdata'])) { + $microdata = $entity->{$field['field_name']}['#microdata']; + } + else { + $subfields = addressfield_data_property_info(); + $microdata['#url'] = ''; + foreach ($subfields as $subfield_name => $subfield) { + $microdata[$subfield_name] = ''; + } + } + switch ($display['type']) { case 'addressfield_default': foreach ($items as $delta => $item) { $element[$delta] = array( '#theme' => array('addressfield_formatter__' . $item['country'], 'addressfield_formatter'), '#address' => $item, + '#microdata' => $microdata, ); } break; @@ -700,6 +762,7 @@ function addressfield_field_formatter_view($entity_type, $entity, $field, $insta * Preprocess function for the addressfield formatter. */ function template_preprocess_addressfield_formatter(&$variables) { + unset($variables['address']['#microdata']); // Encode the address elements. $variables['address'] = array_map('check_plain', $variables['address']); @@ -783,6 +846,7 @@ function addressfield_data_property_info($name = NULL) { 'type' => 'text', 'getter callback' => 'entity_property_verbatim_get', 'setter callback' => 'entity_property_verbatim_set', + 'microdata enabled' => TRUE, ); } diff --git a/theme/addressfield-formatter--US.tpl.php b/theme/addressfield-formatter--US.tpl.php index ffe4c77..1064a7a 100644 --- a/theme/addressfield-formatter--US.tpl.php +++ b/theme/addressfield-formatter--US.tpl.php @@ -7,9 +7,9 @@ ?>
+ >
', array_filter(array( - !empty($address['name_line']) ? $address['name_line'] : $address['first_name'] . ' ' . $address['last_name'], $address['organisation_name'], $address['thoroughfare'], $address['premise'], @@ -19,11 +19,11 @@ ?>
- - - + > + > + >
- + >
diff --git a/theme/addressfield-formatter.tpl.php b/theme/addressfield-formatter.tpl.php index dcaf417..e8211bf 100644 --- a/theme/addressfield-formatter.tpl.php +++ b/theme/addressfield-formatter.tpl.php @@ -7,24 +7,20 @@ ?>
- ', array_filter(array( - !empty($address['name_line']) ? $address['name_line'] : $address['first_name'] . ' ' . $address['last_name'], - $address['organisation_name'], - $address['thoroughfare'], - $address['premise'], - $address['sub_premise'], - $address['dependent_locality'], - ))); - ?> + >
+ >
+ >
+ >
+ >
+ >
- + > >
- + >
- + >