You are here

function getlocations_fields_tokens in Get Locations 7

Same name and namespace in other branches
  1. 7.2 modules/getlocations_fields/getlocations_fields.functions.inc \getlocations_fields_tokens()

Implements hook_tokens().

File

modules/getlocations_fields/getlocations_fields.functions.inc, line 754
getlocations_fields.functions.inc @author Bob Hutchinson http://drupal.org/user/52366 @copyright GNU GPL

Code

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;
}