You are here

function flag_tokens in Flag 7.3

Same name and namespace in other branches
  1. 8.4 flag.tokens.inc \flag_tokens()
  2. 7.2 flag.tokens.inc \flag_tokens()

Implements hook_tokens().

File

./flag.tokens.inc, line 119
Flag module tokens support.

Code

function flag_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  $sanitize = !empty($options['sanitize']);
  $langcode = isset($options['language']) ? $options['language']->language : NULL;
  if ($type == 'flag' && !empty($data['flag'])) {
    $flag = $data['flag'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'name':
          $replacements[$original] = $sanitize ? check_plain($flag->name) : $flag->name;
          break;
        case 'title':
          $replacements[$original] = $sanitize ? check_plain($flag
            ->get_title()) : $flag
            ->get_title();
          break;
      }
    }
  }
  elseif ($type == 'flagging' && !empty($data['flagging'])) {
    $flagging = $data['flagging'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'date':
          $replacements[$original] = format_date($flagging->timestamp, 'medium', '', NULL, $langcode);
          break;
      }
    }
    if ($date_tokens = token_find_with_prefix($tokens, 'date')) {
      $replacements += token_generate('date', $date_tokens, array(
        'date' => $flagging->timestamp,
      ), $options);
    }
  }
  elseif ($type == 'flag-action' && !empty($data['flag-action'])) {
    $action = $data['flag-action'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'action':
          $replacements[$original] = $action->action;
          break;
        case 'entity-url':
          $replacements[$original] = $sanitize ? check_url($action->entity_url) : $action->entity_url;
          break;
        case 'entity-title':
          $replacements[$original] = $sanitize ? check_plain($action->entity_title) : $action->entity_title;
          break;
        case 'entity-type':
          $replacements[$original] = $action->entity_type;
          break;
        case 'entity-id':
          $replacements[$original] = $action->entity_id;
          break;
        case 'count':
          $replacements[$original] = $action->count;
          break;
      }
    }
  }

  // We only provide tokens on entity types if we have token module's helper
  // methods available.
  if (isset($data[$type]) && module_exists('token')) {
    $entity_type = token_get_entity_mapping('token', $type);
    if ($entity_type && in_array($entity_type, flag_get_types())) {
      $flags = flag_get_flags($entity_type);
      $object = $data[$type];
      foreach ($flags as $flag) {
        foreach ($tokens as $name => $original) {
          $flag_count_token = 'flag-' . str_replace('_', '-', $flag->name) . '-count';
          $flag_link_token = 'flag-' . str_replace('_', '-', $flag->name) . '-link';
          if ($name == $flag_count_token) {
            $replacements[$original] = $flag
              ->get_count($flag
              ->get_entity_id($object));
          }
          elseif ($name == $flag_link_token) {
            $replacements[$original] = flag_create_link($flag->name, $flag
              ->get_entity_id($object));
          }
        }
      }
    }
  }
  return $replacements;
}