It would be great if we could use tokens to access particular components of an address stored using Get Locations Fields. Currently there is a token available for the field as a whole, which seems much less useful.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

hutch’s picture

I know very little about Tokens, I can find docs that tell me I need a hook_token_info and a hook_tokens, there is an example_token module in the examples module but I do not have any idea how to test, if you can fill me in with details how to test a token and help with testing once I think I've got it that would help a lot.

mandclu’s picture

I can't speak generally about testing Tokens. I do tend to use tokens most often with Pathauto or rewriting the output of fields in Views. Woukd be happy to help with testing/troubleshooting.

hutch’s picture

I have attached getlocations_fields.tokens.txt, this should be downloaded and the contents added to the end of getlocations_fields.module. Use current dev, that is what this has been developed on and be sure to flush the cache at least once.
The Views module has its own replacement tokens, these are available anyway. I used the Link module as I could test with it without wrecking the menu system which might/would have been the case with pathauto. A link field was added to a content type that also has a getlocations_fields instance and I used the link content textfield to test the token, that way nothing bad was likely to occur when I got it wrong which I did many times before it started to make sense.

It has been tested with nodes only, no other entities as yet but I have allowed for other entities that have getlocations_fields instances in them.
The tokens are formed in the following manner:
[<entity>:<field>:<item>:<delta>]
eg
[node:field_location:name:0]

The above assumes that the fieldname you have given the instance is "field_location" and will show the name field from the first map.
The following items are supported:
'name'
'street'
'additional'
'city'
'province'
'postal_code'
'latitude'
'longitude'
'country'
'country_name'
'phone'
'mobile'
'fax'

Please inform this issue on your testing experience!

mandclu’s picture

Thanks for the quick turnaround! Just to confirm, should this work with either the 1.x or 2.x dev branch, or is there a specific one I should use for testing?

mandclu’s picture

I'm guessing it must have been developed for the 2.x branch. When I tested with 1.x-dev I got a fatal error:
Fatal error: Call to undefined function getlocations_get_supported_entities() in /Users/martin/Sites/budweisergardens/sites/all/modules/getlocations/modules/getlocations_fields/getlocations_fields.module on line 4895

hutch’s picture

I tested on 1.x
I have fixed the typo in the included attachment.
I might work on 2.x but first things first

hutch’s picture

Works in 2.x dev as well. Will work in stable too I imagine.

mandclu’s picture

I can confirm that the code in #6 works as intended. I also tested omitting the delta and confirmed that it does indeed default to 0. Great work! I'd be happy to roll a patch if that would help.

hutch’s picture

Good to have a confirmation, I will put the functions where I want them :-)

Tokens support has now been commited to 1.x dev and 2.x dev

cimo75’s picture

Hi
7.x-1.x-dev can't get token to show up. They appear properly in the token browser but if I try to print any, none show up, using Display Suite fields and the DS input filter.
The tokens are copy and paste from the token browser, just exchanging the "?" for a "0". What am I missing here?
tx
Simone

hutch’s picture

I eventually found a way to test this feature, using a module token_filter which adds token to formats in textarea boxes.
My test site works without this change but I think it might be the cause:
In getlocations_fields.functions.inc line 765 change:

$lang = (isset($obj->language) ? $obj->language : LANGUAGE_NONE);

to

$lang = (isset($obj->language) && $obj->language ? $obj->language : LANGUAGE_NONE);

Tested with

[node:field_location:name]
[node:field_location:name:0]
[user:field_location:name]
[user:field_location:name:0]

Where "field_location" is the machine name for a getlocations_fields instance in a content type.
Also tested street, country and country_name

cimo75’s picture

Hi
I tried the fix and a number of tokens for the node, the current user and the user but none will display.
S.

hutch’s picture

I should point out that there is no mention of support for the display suite module on the Token module's list of supporting modules. I did try the display suite on one occasion some time ago, it crashed the site and I had to roll back to backups so I won't be doing that again.

cimo75’s picture

Hi,
I tried the Token Input Filter both on a block and on the body of a node but these tokens are not printed..
tx
S.

hutch’s picture

Make sure you have enabled the token filter in a HTML format eg Full HTML

cimo75’s picture

Yes I ve enabled it, although in the PHP input filter. What I am doing next is to set up a clean new install of Drupal and see how it goes there.
tx
S.

cimo75’s picture

On a fresh install, node tokens work correctly but user and current-user don't work, I am loading them in a body field.
tx
Simone

cimo75’s picture

Once again with a fresh install this is the body of a basic page node, you can see how the user related getlocation tokens are not rendered, i ve added a test field in the user fields and it does work:

Additional	Additional
City	San Francisco	City
Country	US	Country
Country name	United States	Country name
Fax	Fax
Latitude	37.785236	Latitude
Longitude	-122.396333	Longitude
Mobile	Mobile
Name	2nd St	Name
Phone	Phone
Postal code	Postal code
Province	San Francisco County, California	Province
Street	2nd St

-----------------------

Additional	[current-user:field_location:additional]	Additional
City	[current-user:field_location:city]	City
Country	[current-user:field_location:country]	Country
Country name	[current-user:field_location:country_name]	Country name
Fax	[current-user:field_location:fax]	Fax
Latitude	[current-user:field_location:latitude]	Latitude
Longitude	[current-user:field_location:longitude]	Longitude
Mobile	[current-user:field_location:mobile]	Mobile
Name	[current-user:field_location:name]	Name
Phone	[current-user:field_location:phone]	Phone
Postal code	[current-user:field_location:postal_code]	Postal code
Province	[current-user:field_location:province]	Province
Street	[current-user:field_location:street]	Street
-
User name: io
Test field: this is a simple text field

-----------------------

Additional	[user:field_location:additional]	Additional
City	[user:field_location:city]	City
Country	[user:field_location:country]	Country
Country name	[user:field_location:country_name]	Country name
Fax	[user:field_location:fax]	Fax
Latitude	[user:field_location:latitude]	Latitude
Longitude	[user:field_location:longitude]	Longitude
Mobile	[user:field_location:mobile]	Mobile
Name	[user:field_location:name]	Name
Phone	[user:field_location:phone]	Phone
Postal code	[user:field_location:postal_code]	Postal code
Province	[user:field_location:province]	Province
Street	[user:field_location:street]	Street

---------------------
cimo75’s picture

I can print properly the current user latitude like so:

global $user;
$user_data = user_load($user->uid);
$user_latitude = $user_data->field_location['und'][0]['latitude'];
print $user_latitude;
hutch’s picture

Here is a new version of function getlocations_fields_tokens fron getlocations_fields.functions.inc

function getlocations_fields_tokens($type, $tokens, array $data = array(), array $options = array()) {

  // Setup our replacements array.
  $sanitize = ( isset($options['sanitize']) && $options['sanitize'] ? TRUE : FALSE);
  $replacements = array();
  $module = 'getlocations_fields';
  $obj = FALSE;

  // Make sure we have an entity.
  if (isset($data['entity_type']) && isset($data['entity'])) {

    $obj = $data['entity'];
    $lang = (isset($obj->language) && $obj->language ? $obj->language : LANGUAGE_NONE);

  }

  if ($obj) {
    // Loop through the tokens.
    foreach ($tokens as $name => $original) {
      // Break token into an array.
      $pieces = explode(':', str_replace(array('[', ']'), '', $original));

      // Must have entity, field, and item to be a getlocations_fields token.
      if (count($pieces) < 3) {
        continue;
      }
      if (count($pieces) > 3 && $pieces[0] == 'user' && $pieces[1] == 'original' ) {
        $entity = $pieces[0];
        $field = $pieces[2];
        $item = $pieces[3];
        $delta = (isset($pieces[4]) && is_numeric($pieces[4]) ? $pieces[4] : 0);
      }
      else {
        $entity = $pieces[0];
        $field = $pieces[1];
        $item = $pieces[2];
        $delta = (isset($pieces[3]) && is_numeric($pieces[3]) ? $pieces[3] : 0);
      }

      $field_info = field_info_field($field);
      $supported_entities = getlocations_get_supported_entities($module);
      if ($entity == 'current-user') {
        $supported_entities[] = 'current-user';
      }
      $fieldnames = getlocations_get_fieldnames();
      $col_keys = getlocations_fields_columns(TRUE);
      // Make sure this is a field and it is a getlocations_fields field.
      if (!isset($field_info) || (isset($field_info) && $field_info['type'] != $module)
        || ! in_array($field, $fieldnames)
        || ! in_array($item, $col_keys)
        || ! in_array($entity, $supported_entities)
        ) {
        continue;
      }
      $content = (isset($obj->{$field}[$lang][$delta][$item]) ? $obj->{$field}[$lang][$delta][$item] : '');
      if ($content && $sanitize) {
        $content = getlocations_apoclean($content);
      }
      $replacements[$original] = $content;
    }
  }

  return $replacements;

}

This will support current-user and tokens like [user:original:field_address:name:0]
"current-user" will work in node, "user" will not.

cimo75’s picture

Ok working correctly on 7.x-1.x-dev ! Thanks for the support.
S.

hutch’s picture

The fix in #20 and some other stuff commited to getlocations-1.x-dev and getlocations-2.x-dev.

cimo75’s picture

Hi
now I went back to the first site I was working on, I removed completely getlocations and readded the fields, I have same Token, Token Input, Getlocations Fields module, same Core Drupal, I analyzed the tables structure and it all seems fine there, same input filter. But in this site none of the Field Location tokens are printed ... I ve spent some time over this issue and I am at a loss, any idea?
tx
Simone

cimo75’s picture

Hallo, good morning,;)
I tried to keep an eye open on the tokens being printed while I worked on the site but, at some point, the current-user tokens disappeared again.. and I am not sure what caused them to disappear so no big help on debugging at the moment.
After installing latest 7.x-1.x-dev from 25/08 I noticed this error too:

screenshot

the only notice I keep getting is :

Notice: Undefined variable: key en getlocations_fields_load_location() (línea 2519 de /home/marta/public_html/drupal/sites/all/modules/getlocations/modules/getlocations_fields/getlocations_fields.module).
Notice: Undefined variable: key en getlocations_fields_load_location() (línea 2523 de /home/marta/public_html/drupal/sites/all/modules/getlocations/modules/getlocations_fields/getlocations_fields.module).
Notice: Undefined variable: key en getlocations_fields_load_location() (línea 2527 de /home/marta/public_html/drupal/sites/all/modules/getlocations/modules/getlocations_fields/getlocations_fields.module).
Notice: Undefined variable: key en getlocations_fields_load_location() (línea 2531 de /home/marta/public_html/drupal/sites/all/modules/getlocations/modules/getlocations_fields/getlocations_fields.module).
hutch’s picture

The section of code from line 2518 to line 2535 can be commented out, these are notices and do not interfere with the running. In my test rig current-user is working and node is working in nodes and user, current-user and user:original are working in user.

cimo75’s picture

Yes that must be something along the way on this site that blocks the current-user token on nodes. I may try to disable modules one by one and see what happens, anyway I go the function I wanted (directions from current user to node) to work with the php snippet posted before, and it is working with DS fields so Display Suite can be used safely.
S.

hutch’s picture

If you want a map showing directions you could try the Getdirections module which supports Getlocations_fields in a variety of ways.

cimo75’s picture

Yes that' s what I am using, a combination of the two modules.
S.

hutch’s picture

In that case see function getdirections_location_u2n_path in getdirections.api.inc which will give you a path printable in PHP if you have $uid and $nid.

cimo75’s picture

I did something like that:


global $user;
$user_data = user_load($user->uid);
$user_latitude = $user_data->field_location['und'][0]['latitude'];
$user_longitude = $user_data->field_location['und'][0]['longitude'];
$user_locname = $user_data->field_location['und'][0]['name'];

$fromlocs = $user_locname;
$fromlatlon = $user_latitude . "," . $user_longitude;
$tolocs = "[node:field_location:name]";
$tolatlon = "[node:field_location:latitude],[node:field_location:longitude]";

return getdirections_locations_bylatlon($fromlocs, $fromlatlon, $tolocs, $tolatlon);

albee’s picture

I have tested getlocations_fields.tokens2.txt and getlocations_fields.tokens.txt on 7x-1.17.dev I get this error
Fatal error: Cannot redeclare getlocations_fields_token_info() (previously declared in /sites/all/modules/getlocations/getlocations.module:8045) in /sites/all/modules/getlocations/modules/getlocations_fields/getlocations_fields.functions.inc on line 749

If i use 7x.1.16 module I get this Fatal error: Call to undefined function getlocations_get_supported_entities() in /sites/all/modules/getlocations/getlocations.module on line 7722

hutch’s picture

Please read the whole of this issue, the two functions were commited two years ago, see #22
You should always use a current version of Getlocations, either stable or dev
If you use the Token_filter module you will be able to use token placeholders in a body as suggested above.
eg
[node:field_location:country]

albee’s picture

Thank you hutch. I've sorted it out. But.. [node:field_location:country] is not works... [node:field-location:country] works well. The different is _ & - There are both examples in tokens list.