You are here

function pcp_tokens in Profile Complete Percent 7

Implements hook_tokens().

Provide a user token for the Profile Completed Percentage. See system.api.php and token.tokens.inc for good examples.

File

./pcp.tokens.inc, line 45
Builds replacement tokens for Profile Complete Percentage.

Code

function pcp_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  switch ($type) {
    case 'pcp-user':
      if (empty($data['user'])) {
        break;
      }
      $account = $data['user'];

      // If $original == '[site:current-user:pcp-user]', $name == 'pcp-user'.
      foreach ($tokens as $name => $original) {
        switch ($name) {
          case 'pcp-user':
            $data = pcp_get_complete_percentage_data('user', 'user', $account);
            $replacements[$original] = $data['current_percent'];
            break;
        }
      }
      if ($profile2_tokens = token_find_with_prefix($tokens, 'pcp-profile2')) {
        foreach ($profile2_tokens as $name => $original) {

          // If $original == '[site:current-user:pcp-profile2:foo]', then
          // $name == 'foo'.
          $data = pcp_get_complete_percentage_data('profile2', $name, $account);
          $replacements[$original] = $data['current_percent'];
        }
      }
      break;
  }
  return $replacements;
}