You are here

function countries_tokens in Countries 7

Same name and namespace in other branches
  1. 8 countries.tokens.inc \countries_tokens()
  2. 7.2 countries.tokens.inc \countries_tokens()

Implements hook_tokens().

File

./countries.tokens.inc, line 70
Builds placeholder replacement tokens for country-related data.

Code

function countries_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $sanitize = !empty($options['sanitize']);
  $replacements = array();
  if ($type == 'country' && !empty($data['country'])) {
    $country = $data['country'];
    foreach ($tokens as $name => $original) {
      switch ($name) {

        // Basic country information.
        case 'cid':
          $replacements[$original] = $country->cid;
          break;
        case 'name':
        case 'official-name':
        case 'iso2':
        case 'iso3':
          $property = str_replace('-', '_', $name);
          $replacements[$original] = $sanitize ? check_plain($country->{$property}) : $country->{$property};
          break;
        case 'continent':
          $continents = countries_get_continents();
          $continent = isset($continents[$country->continent]) ? $continents[$country->continent] : '';
          $replacements[$original] = $sanitize ? check_plain(${$continent}) : $continent;
          break;
        case 'continent-code':
          $replacements[$original] = $sanitize ? check_plain($country->continent) : $country->continent;
          break;
        case 'numcode':
          $replacements[$original] = theme('countries_number', array(
            'country' => $country,
          ));
          break;
        case 'enabled':
          $replacements[$original] = $country->enabled ? t('Enabled') : t('Disabled');
          break;
      }
    }
  }
  if ($type == 'default-country') {
    $country = country_load(variable_get('site_default_country', ''));
    $replacements += token_generate('country', $tokens, array(
      'country' => $country,
    ), $options);
  }
  return $replacements;
}