diff --git a/geofield.formatters.inc b/geofield.formatters.inc
index d9854d1..263f6a4 100644
--- a/geofield.formatters.inc
+++ b/geofield.formatters.inc
@@ -252,7 +252,8 @@ function geofield_field_formatter_view($entity_type, $entity, $field, $instance,
switch ($display['type']) {
case 'geofield_wkt':
foreach ($items as $delta => $item) {
- $element[$delta] = array('#markup' => $item['wkt']);
+ $microdata = _geofield_item_microdata_propertyvalue($entity, $instance, $item);
+ $element[$delta] = array('#markup' => $item['wkt'] . $microdata);
}
return $element;
case 'geofield_geojson':
@@ -281,22 +282,25 @@ function geofield_field_formatter_view($entity_type, $entity, $field, $instance,
return $element;
case 'geofield_latlon':
foreach ($items as $delta => $item) {
+ $microdata = _geofield_item_microdata_propertyvalue($entity, $instance, $item);
if ($display['settings']['labels']) {
- $element[$delta] = array('#markup' => t('Latitude: !latitude
Longitude: !longitude', array('!latitude' => $item['lat'], '!longitude' => $item['lon'])));
+ $element[$delta] = array('#markup' => t('Latitude: !latitude
Longitude: !longitude' . $microdata, array('!latitude' => $item['lat'], '!longitude' => $item['lon'])));
}
else {
- $element[$delta] = array('#markup' => $item['lat'] . ', ' . $item['lon']);
+ $element[$delta] = array('#markup' => $item['lat'] . ', ' . $item['lon'] . $microdata);
}
}
return $element;
case 'geofield_lat':
foreach ($items as $delta => $item) {
- $element[$delta] = array('#markup' => $item['lat']);
+ $microdata = _geofield_item_microdata_propertyvalue($entity, $instance, $item);
+ $element[$delta] = array('#markup' => $item['lat'] . $microdata);
}
return $element;
case 'geofield_lon':
foreach ($items as $delta => $item) {
- $element[$delta] = array('#markup' => $item['lon']);
+ $microdata = _geofield_item_microdata_propertyvalue($entity, $instance, $item);
+ $element[$delta] = array('#markup' => $item['lon'] . $microdata);
}
return $element;
case 'geofield_geo_type':
@@ -323,6 +327,39 @@ function geofield_field_formatter_view($entity_type, $entity, $field, $instance,
return $element;
}
+/**
+ * Function to return appropriate GeoCoordinates/GeoShape properties.
+ *
+ * This is developing code, as an example. Output can be made available to the
+ * theme, as specific site may want to display or not data based on other
+ * factors.
+ */
+function _geofield_item_microdata_propertyvalue($entity, $instance, $item) {
+ $output = '';
+
+ // If there is no microdata mapping for the field, return an empty string.
+ if (isset($entity->microdata[$instance['field_name']])) {
+ $microdata = $entity->microdata[$instance['field_name']];
+ }
+ else {
+ return '';
+ }
+
+ if ($item['geo_type'] == 'point') {
+ foreach (array('lat', 'lon') as $property) {
+ $microdata[$property]['#attributes']['content'] = $item[$property];
+ $output .= '';
+ }
+ }
+ else {
+ $schemaorg_shape = geofield_schemaorg_shape($item);
+ $microdata['schemaorg_shape']['#attributes']['content'] = $schemaorg_shape;
+ $output .= '';
+ }
+
+ return $output;
+}
+
function _geofield_openlayers_formatter($map_name, $items) {
$features = array();
diff --git a/geofield.module b/geofield.module
index 772dc06..6956774 100644
--- a/geofield.module
+++ b/geofield.module
@@ -25,6 +25,7 @@ function geofield_field_info() {
),
'property_type' => 'geofield',
'property_callbacks' => array('geofield_property_info_callback'),
+ 'microdata' => TRUE,
),
);
}
@@ -148,6 +149,72 @@ function geofield_field_instance_settings_form($field, $instance) {
}
/**
+ * Implements hook_microdata_suggestions().
+ */
+function geofield_microdata_suggestions() {
+ $suggestions = array();
+
+ // Standard suggestion for co-ordinates.
+ // http://schema.org/GeoCoordinates
+ //
+ // Output example:
+ //
+ // Latitude: 40 deg 44 min 54.36 sec N
+ // Longitude: 73 deg 59 min 8.5 dec W
+ //
+ //
+ //
+ $suggestions['fields']['geofield']['schema.org/GeoCoordinates'] = array(
+ '#itemprop' => array('geo'),
+ '#is_item' => TRUE,
+ '#itemtype' => array('http://schema.org/GeoCoordinates'),
+ 'wkt' => array(
+ '#itemprop' => array('latitude', 'longitude'),
+ ),
+ 'lat' => array(
+ '#itemprop' => array('latitude'),
+ ),
+ 'lon' => array(
+ '#itemprop' => array('longitude'),
+ ),
+ 'latlon' => array(
+ '#itemprop' => array('latitude', 'longitude'),
+ ),
+ );
+
+ // Suggestion for more complicated shapes.
+ // http://schema.org/GeoShape
+ //
+ // Output example:
+ //
+ // WKT: LINESTRING (30 10, 10 30, 40 40)
+ //
+ //
+ $shape_base = array(
+ '#itemprop' => array('geo'),
+ '#is_item' => TRUE,
+ '#itemtype' => array('http://schema.org/GeoShape'),
+ );
+
+ // Suggestion for a line.
+ $suggestions['fields']['geofield']['schema.org/GeoShape line'] = $shape_base;
+ $suggestions['fields']['geofield']['schema.org/GeoShape line']['schemaorg_shape']['#itemprop'] = array('line');
+
+ // Suggestion for a polygon.
+ $suggestions['fields']['geofield']['schema.org/GeoShape polygon'] = $shape_base;
+ $suggestions['fields']['geofield']['schema.org/GeoShape polygon']['schemaorg_shape']['#itemprop'] = array('polygon');
+
+ return $suggestions;
+}
+
+/**
+ * implements hook_microdata_value_types_alter().
+ */
+function geofield_microdata_value_types_alter(&$types) {
+ $types['geofield'] = 'item_option';
+}
+
+/**
* Geofield Compute Values
*
* Compute all dependant values. We compute all other values from whichever
@@ -431,6 +498,33 @@ function geofield_latlon_DECtoCCS($dec, $axis) {
}
/**
+ * Schema.org format.
+ *
+ * Formats shapes for output in metadata using Schema.org format. This is
+ * different from the WKT format provided by GeoGenerator, so just use a one
+ * off function.
+ */
+function geofield_schemaorg_shape($item) {
+ $output = '';
+ $bottom = $item['bottom'];
+ $left = $item['left'];
+ $right = $item['right'];
+ $top = $item['top'];
+
+ switch ($item['geo_type']) {
+ case 'polygon':
+ $output = $bottom . ',' . $left . ' ';
+ $output .= $bottom . ',' . $right . ' ';
+ $output .= $top . ',' . $right . ' ';
+ $output .= $top . ',' . $left . ' ';
+ $output .= $bottom . ',' . $left;
+ break;
+ }
+
+ return $output;
+}
+
+/**
* Callback to alter the property info of geofield fields.
*
* @see geofield_field_info()
@@ -444,7 +538,6 @@ function geofield_property_info_callback(&$info, $entity_type, $field, $instance
$property['setter callback'] = 'entity_metadata_field_verbatim_set';
// $property['auto creation'] = 'geofield_default_values';
$property['property info'] = geofield_data_property_info('Geofield');
-
unset($property['query callback']);
}
@@ -453,56 +546,78 @@ function geofield_property_info_callback(&$info, $entity_type, $field, $instance
*/
function geofield_data_property_info($name = NULL) {
// Build an array of basic property information for the geofield field.
+
+ // If any of these should have individual options to set microdata
+ // properties is not clear. Lat, Lon work fine. Other data is more
+ // complicated. Geo_type relates (not 1:1) with the itemprop within
+ // a GeoShape, with an alternate representation of the wkt for the content
+ // (at its simplest).
$properties = array(
'wkt' => array(
'label' => 'Well-known text',
'type' => 'text',
+ 'microdata' => FALSE,
),
'geo_type' => array(
'label' => 'Geo Type',
'options list' => '_geofield_geo_types_options_callback',
'required' => TRUE,
+ 'microdata' => FALSE,
),
'lat' => array(
'label' => 'Latitude',
'type' => 'decimal',
'required' => TRUE,
'setter callback' => 'entity_property_verbatim_set',
+ 'microdata' => TRUE,
),
'lon' => array(
'label' => 'Longitude',
'type' => 'decimal',
'required' => TRUE,
'setter callback' => 'entity_property_verbatim_set',
+ 'microdata' => TRUE,
),
'left' => array(
'label' => 'Left Latitude',
'type' => 'decimal',
'setter callback' => 'entity_property_verbatim_set',
+ 'microdata' => FALSE,
),
'top' => array(
'label' => 'Top Longitude',
'type' => 'decimal',
'setter callback' => 'entity_property_verbatim_set',
+ 'microdata' => FALSE,
),
'right' => array(
'label' => 'Right Latitude',
'type' => 'decimal',
'setter callback' => 'entity_property_verbatim_set',
+ 'microdata' => FALSE,
),
'bottom' => array(
'label' => 'Bottom Longitude',
'type' => 'decimal',
'setter callback' => 'entity_property_verbatim_set',
+ 'microdata' => FALSE,
),
'srid' => array(
'label' => 'Projection (SRID)',
- 'type' => 'integer'
+ 'type' => 'integer',
+ 'microdata' => FALSE,
),
'latlon' => array(
'label' => 'LatLong Pair',
'type' => 'string',
'getter callback' => 'geofield_return_latlon',
+ 'microdata' => FALSE,
+ ),
+ 'schemaorg_shape' => array(
+ 'label' => 'Schema.org Shape',
+ 'type' => 'string',
+ 'getter callback' => 'geofield_return_schemaorg_shape',
+ 'microdata' => TRUE,
),
);
@@ -539,6 +654,16 @@ function geofield_return_latlon($data, array $options, $name) {
}
/**
+ * Gets the Schema.org formatted shape value.
+ */
+function geofield_return_schemaorg_shape($data, array $options, $name) {
+ if ((is_array($data) || (is_object($data) && $data instanceof ArrayAccess))) {
+ return geofield_schemaorg_shape($data);
+ }
+ return NULL;
+}
+
+/**
* Haversine formula, useful for injecting into an SQL statement. In instances where it isn't possible to pass in variables dynamically (i.e. field
* definitions), this function will bake in values directly into the snippet.
*