You are here

function location_cck_tokens in Location 7.3

Implements hook_tokens().

File

contrib/location_cck/location_cck.module, line 656
Defines location field type.

Code

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

  // Setup our replacements array.
  $replacements = array();

  // Make sure we have a node.
  if (isset($data['entity_type']) && $data['entity_type'] == 'node') {
    $node = $data['entity'];

    // Get the available fields from the location module.
    $available = location_field_names(TRUE);

    // 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 location cck token.
      if (count($pieces) < 3) {
        continue;
      }
      $entity = $pieces[0];
      $field = $pieces[1];
      $field_info = field_info_field($field);

      // Make sure this is a field and it is a location field.
      if (!isset($field_info) || isset($field_info) && $field_info['type'] != 'location') {
        continue;
      }
      $item = $pieces[2];

      // Allow for a position at the end of the token. If no position is given
      // default it to the first element.
      $position = isset($pieces[3]) && is_numeric($pieces[3]) ? $pieces[3] : 0;

      // If the token is in the $available, replace it.
      if (array_key_exists($item, $available) && $available[$item]) {
        $replacements[$original] = isset($node->{$field}[LANGUAGE_NONE][$position][$item]) ? $node->{$field}[LANGUAGE_NONE][$position][$item] : '';
      }
    }
  }
  return $replacements;
}