You are here

function token_entity_info_alter in Token 7

Implements hook_entity_info_alter().

Because some token types to do not match their entity type names, we have to map them to the proper type. This is purely for other modules' benefit.

See also

token_get_entity_mapping()

http://drupal.org/node/737726

File

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

Code

function token_entity_info_alter(&$info) {
  foreach (array_keys($info) as $entity_type) {

    // Add a token view mode if it does not already exist. Only work with
    // fieldable entities.
    if (!empty($info[$entity_type]['fieldable'])) {
      if (!isset($info[$entity_type])) {
        $info[$entity_type]['view modes'] = array();
      }
      if (!isset($info[$entity_type]['view modes']['token'])) {
        $info[$entity_type]['view modes']['token'] = array(
          'label' => t('Tokens'),
          'custom settings' => FALSE,
        );
      }
    }
    if (!empty($info[$entity_type]['token type'])) {

      // If the entity's token type is already defined, great!
      continue;
    }

    // Fill in default token types for entities.
    switch ($entity_type) {
      case 'taxonomy_term':
      case 'taxonomy_vocabulary':

        // Stupid taxonomy token types...
        $info[$entity_type]['token type'] = str_replace('taxonomy_', '', $entity_type);
        break;
      default:

        // By default the token type is the same as the entity type.
        $info[$entity_type]['token type'] = $entity_type;
        break;
    }
  }
}