You are here

function token_get_entity_mapping in Token 7

Return an array of entity type to token type mappings.

Why do we need this? Because when the token API was moved to core we did not re-use the entity type as the base name for taxonomy terms and vocabulary tokens.

See also

token_entity_info_alter()

http://drupal.org/node/737726

4 calls to token_get_entity_mapping()
TokenEntityTestCase::testEntityMapping in ./token.test
token_build_tree in ./token.module
Build a tree array of tokens used for themeing or information.
token_tokens in ./token.tokens.inc
Implements hook_tokens().
_token_field_info in ./token.tokens.inc
Fetch an array of field data used for tokens.
1 string reference to 'token_get_entity_mapping'
token_clear_cache in ./token.module
Clear token caches and static variables.

File

./token.module, line 387
Enhances the token API in core: adds a browseable UI, missing tokens, etc.

Code

function token_get_entity_mapping($value_type = 'token', $value = NULL, $fallback = FALSE) {
  $mapping =& drupal_static(__FUNCTION__, array());
  if (empty($mapping)) {
    foreach (entity_get_info() as $entity_type => $info) {
      $mapping[$entity_type] = !empty($info['token type']) ? $info['token type'] : $entity_type;
    }

    // Allow modules to alter the mapping array.
    drupal_alter('token_entity_mapping', $mapping);
  }
  if (!isset($value)) {
    return $value_type == 'token' ? array_flip($mapping) : $mapping;
  }
  elseif ($value_type == 'token') {
    $return = array_search($value, $mapping);
    return $return !== FALSE ? $return : ($fallback ? $value : FALSE);
  }
  elseif ($value_type == 'entity') {
    return isset($mapping[$value]) ? $mapping[$value] : ($fallback ? $value : FALSE);
  }
}