Not sure where this should be as in which issue queue, geofield or entity api, see related issues.

This is a workaround for this particular issue, not a patch or fix. It works but needs amendment for the field name.

I have a geofield field attached to a content type in a geocoded node.
The metatags geo.position should be taking the latitude and longitude to sufficient significant figures to geocode in the metatag. But as reported elsewhere entity api decimalises the values and shows them only to two significant digits.

So what should read <meta name="geo.position" content="51.973190500000;0.862229300000"> ends up as <meta name="geo.position" content="51.97;0.86"> This used the tokens [node:field-geolocation:lat];[node:field-geolocation:lon]
(Interestingly as an aside @rachel_norfolk demonstrated that in drupal 8 this is comma delimited rather than semicolon delimited so not sure which is correct. Is this another issue?)

So having experience of writing custom tokens I wrote the following as a workaround (the node content field that geocodes is called field_geolocation so change to whatever your field name is and change $node to your own custom entity if needed.

<?php
/**
 * Implements hook_token_info(). This hook will register tow token lname and fname.
 */
function geo_metatokens_token_info() {
	$info = array();
	$info['types']['geofield_tokens'] = array(
		'name' => t('Geofield Tokens'),
    'description' => t('Geofield custom tokens.'),
	);
  $info['tokens']['geofield_tokens']['geo_position'] = array(
    'name' => t('geo.position'),
    'description' => t('geo.position metatag for Geofield. semicolon delimiter'),
  );
  $info['tokens']['geofield_tokens']['geo_latitude'] = array(
    'name' => t('geo latitude'),
    'description' => t('geofield latitude token.'),
  );
    $info['tokens']['geofield_tokens']['geo_longitude'] = array(
    'name' => t('geo latitude'),
    'description' => t('geofield longitude token.'),
  );
    $info['tokens']['geofield_tokens']['geo_icbm'] = array(
    'name' => t('geo icbm'),
    'description' => t('geofield icbm format comma delimiter.'),
  );
  return $info;
}
/**
 * Implements hook_tokens().
 */
function geo_metatokens_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  $sanitize = !empty($options['sanitize']);
  if ($type == 'geofield_tokens') {
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'geo_position':
					$latitude=  geo_metatoken_get_latitude();
          $longitude =  geo_metatoken_get_longitude();
          $replacements[$original] = $latitude . ";" . $longitude;
        break;
        case 'geo_icbm':
          $latitude=  geo_metatoken_get_latitude();
          $longitude =  geo_metatoken_get_longitude();
          $replacements[$original] = $latitude . "," . $longitude;
        break;
        case 'geo_latitude':
					$latitude=  geo_metatoken_get_latitude();
          $replacements[$original] = $longitude;
        break;
        case 'geo_longitude':
          $latitude=  geo_metatoken_get_longitude();
          $replacements[$original] = $longitude;
        break;
      }
    }
  }
  return $replacements;
}
/**
* Obtain the latitude from the geofield field
*/
function geo_metatoken_get_latitude() {
  if(arg(0)!="node")
  return "not viewing a node";

  $node = node_load(arg(1));
  return $node->field_geolocation['und'][0]['lat'];
  return $latitude;
}
/**
* Obtain the longitude from the geofield field
*/
function geo_metatoken_get_longitude() {
  if(arg(0)!="node")
  return "not viewing a node";

  $node = node_load(arg(1));
  return $node->field_geolocation['und'][0]['lon'];
}

Comments

TechnoTim2010 created an issue.

TechnoTim2010’s picture

I was a bit hasty, whilst the geo.position metatag worked the code for latitude and longitude was wrong, Correct working code is below.

<?php
/**
 * Implements hook_token_info(). This hook will register tow token lname and fname.
 */
function geo_metatokens_token_info() {
	$info = array();
	$info['types']['geofield_tokens'] = array(
		'name' => t('Geofield Tokens'),
    'description' => t('Geofield custom tokens.'),
	);
  $info['tokens']['geofield_tokens']['geo_position'] = array(
    'name' => t('geo.position'),
    'description' => t('geo.position metatag for Geofield. semicolon delimiter'),
  );
  $info['tokens']['geofield_tokens']['geo_latitude'] = array(
    'name' => t('geo latitude'),
    'description' => t('geofield latitude token.'),
  );
    $info['tokens']['geofield_tokens']['geo_longitude'] = array(
    'name' => t('geo latitude'),
    'description' => t('geofield longitude token.'),
  );
    $info['tokens']['geofield_tokens']['geo_icbm'] = array(
    'name' => t('geo icbm'),
    'description' => t('geofield icbm format comma delimiter.'),
  );
  return $info;
}
/**
 * Implements hook_tokens().
 */
function geo_metatokens_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  $sanitize = !empty($options['sanitize']);
  if ($type == 'geofield_tokens') {
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'geo_position':
					$latitude=  geo_metatoken_get_latitude();
          $longitude =  geo_metatoken_get_longitude();
          $replacements[$original] = $latitude . ";" . $longitude;
        break;
        case 'geo_icbm':
          $latitude=  geo_metatoken_get_latitude();
          $longitude =  geo_metatoken_get_longitude();
          $replacements[$original] = $latitude . "," . $longitude;
        break;
        case 'geo_latitude':
					$latitude=  geo_metatoken_get_latitude();
          $replacements[$original] = $latitude;
        break;
        case 'geo_longitude':
          $longitude=  geo_metatoken_get_longitude();
          $replacements[$original] = $longitude;
        break;
      }
    }
  }
  return $replacements;
}
/**
* Obtain the latitude from the geofield field
*/
function geo_metatoken_get_latitude() {
  if(arg(0)!="node")
  return "not viewing a node";

  $node = node_load(arg(1));
  return $node->field_geolocation['und'][0]['lat'];
}
/**
* Obtain the longitude from the geofield field
*/
function geo_metatoken_get_longitude() {
  if(arg(0)!="node")
  return "not viewing a node";

  $node = node_load(arg(1));
  return $node->field_geolocation['und'][0]['lon'];
}