You are here

function flag_tokens in Flag 7.2

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

Implements hook_tokens().

File

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

Code

function flag_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  $sanitize = !empty($options['sanitize']);
  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 == '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 'content-url':
          $replacements[$original] = $sanitize ? check_url($action->content_url) : $action->content_url;
          break;
        case 'content-title':
          $replacements[$original] = $sanitize ? check_plain($action->content_title) : $action->content_title;
          break;
        case 'content-type':
          $replacements[$original] = $action->content_type;
          break;
        case 'content-id':
          $replacements[$original] = $action->content_id;
          break;
        case 'count':
          $replacements[$original] = $action->count;
          break;
      }
    }
  }
  if (isset($data[$type]) && in_array($type, flag_get_types())) {
    $flags = flag_get_flags($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_content_id($object));
        }
        elseif ($name == $flag_link_token) {
          $replacements[$original] = flag_create_link($flag->name, $flag
            ->get_content_id($object));
        }
      }
    }
  }
  return $replacements;
}