You are here

function flag_token_info in Flag 7.3

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

Implements of hook_token_info().

The tokens we provide on generic entities require token module.

File

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

Code

function flag_token_info() {
  $types = array();
  $tokens = array();

  // Flag tokens.
  $types['flag'] = array(
    'name' => t('Flags'),
    'description' => t('Tokens related to flag data.'),
    'needs-data' => 'flag',
  );
  $tokens['flag']['name'] = array(
    'name' => t('Flag name'),
    'description' => t('The flag machine-readable name.'),
  );
  $tokens['flag']['title'] = array(
    'name' => t('Flag title'),
    'description' => t('The human-readable flag title.'),
  );

  // Flagging tokens.
  //
  // Attached fields are exposed as tokens via some contrib module, but we
  // need to expose other fields ourselves. Currently, 'date' is the only such
  // field we expose.
  $types['flagging'] = array(
    'name' => t('Flaggings'),
    'description' => t('Tokens related to flaggings.'),
    'needs-data' => 'flagging',
  );
  $tokens['flagging']['date'] = array(
    'name' => t('Flagging date'),
    'description' => t('The date an item was flagged.'),
    'type' => 'date',
  );

  // Flag action tokens.
  $types['flag-action'] = array(
    'name' => t('Flag actions'),
    'description' => t('Tokens available in response to a flag action being executed by a user.'),
    'needs-data' => 'flag-action',
  );
  $tokens['flag-action']['action'] = array(
    'name' => t('Flag action'),
    'description' => t('The flagging action taking place, either "flag" or "unflag".'),
  );
  $tokens['flag-action']['entity-url'] = array(
    'name' => t('Flag entity URL'),
    'description' => t('The URL of the entity being flagged.'),
  );
  $tokens['flag-action']['entity-title'] = array(
    'name' => t('Flag entity title'),
    'description' => t('The title of the entity being flagged.'),
  );
  $tokens['flag-action']['entity-type'] = array(
    'name' => t('Flag entity type'),
    'description' => t('The type of entity being flagged, such as <em>node</em> or <em>comment</em>.'),
  );
  $tokens['flag-action']['entity-id'] = array(
    'name' => t('Flag entity ID'),
    'description' => t('The ID of entity being flagged, such as a nid or cid.'),
  );
  $tokens['flag-action']['count'] = array(
    'name' => t('Flag count'),
    'description' => t('The current count total for this flag.'),
  );

  // Add tokens for the flag count available at the entity level.
  // These require token module because we need its helper data and functions
  // to deal with token types that are not the same as the entity types they are
  // for (in particular, terms and vocabularies).
  if (module_exists('token')) {
    $entity_info = entity_get_info();
    foreach (flag_get_types() as $flag_type) {

      // If the flag type is not an entity type then skip.
      if (!isset($entity_info[$flag_type])) {
        continue;
      }

      // The flag type is the entity type, but this is not necessarily the same
      // as the entity's token type.
      $token_type = $entity_info[$flag_type]['token type'];
      $flags = flag_get_flags($flag_type);
      foreach ($flags as $flag) {
        $tokens[$token_type]['flag-' . str_replace('_', '-', $flag->name) . '-count'] = array(
          'name' => t('@flag flag count', array(
            '@flag' => $flag
              ->get_title(),
          )),
          'description' => t('Total flag count for flag @flag', array(
            '@flag' => $flag
              ->get_title(),
          )),
          'flag-type' => $flag_type,
        );
        $tokens[$token_type]['flag-' . str_replace('_', '-', $flag->name) . '-link'] = array(
          'name' => t('@flag flag link', array(
            '@flag' => $flag
              ->get_title(),
          )),
          'description' => t('Flag/unflag link for @flag', array(
            '@flag' => $flag
              ->get_title(),
          )),
          'flag-type' => $flag_type,
        );
      }
    }
  }
  return array(
    'types' => $types,
    'tokens' => $tokens,
  );
}