You are here

function location_tokens in Location 7.3

Implements hook_tokens().

File

./location.tokens.inc, line 100
Make tokens about location available.

Code

function location_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  if ($type == 'node' && ($location_tokens = token_find_with_prefix($tokens, 'location'))) {
    $locations = isset($data['node']->locations) ? $data['node']->locations : array();
    $replacements += token_generate('location', $location_tokens, array(
      'location' => $locations,
    ), $options);
  }
  elseif ($type == 'user' && ($location_tokens = token_find_with_prefix($tokens, 'location'))) {
    $locations = isset($data['user']->locations) ? $data['user']->locations : array();
    $replacements += token_generate('location', $location_tokens, array(
      'location' => $locations,
    ), $options);
  }
  elseif ($type == 'location' && isset($data['location'])) {
    $locations = $data['location'];

    // Make sure that this is an array of locations and not a single location.
    if (isset($locations['lid'])) {
      $locations = array(
        $locations,
      );
    }

    // Handle all tokens that are dynamic. For example, location:name:1. This does
    // not handle location:name. That is handled lower.
    $tokens_helper = array(
      'name' => token_find_with_prefix($tokens, 'name'),
      'street' => token_find_with_prefix($tokens, 'street'),
      'additional' => token_find_with_prefix($tokens, 'additional'),
      'city' => token_find_with_prefix($tokens, 'city'),
      'province' => token_find_with_prefix($tokens, 'province'),
      'province_name' => token_find_with_prefix($tokens, 'province_name'),
      'postal_code' => token_find_with_prefix($tokens, 'postal_code'),
      'latitude' => token_find_with_prefix($tokens, 'latitude'),
      'longitude' => token_find_with_prefix($tokens, 'longitude'),
      'country' => token_find_with_prefix($tokens, 'country'),
      'country_name' => token_find_with_prefix($tokens, 'country_name'),
    );
    foreach ($tokens_helper as $key => $tokens_find) {
      foreach ($tokens_find as $val => $original) {
        $replacements[$original] = _locations_token_helper($locations, $key, $val, $options);
      }
    }

    // Handle values that do not have a specific location on them. For example,
    // handle location:city but not location:city:0 as it is handled above.
    foreach ($tokens as $key => $original) {
      if (isset($tokens_helper[$key])) {
        $replacements[$original] = _locations_token_helper($locations, $key, 0, $options);
      }
    }
  }
  return $replacements;
}