You are here

function token_entity_type_alter in Token 8

Implements hook_entity_type_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

\Drupal\token\TokenEntityMapperInterface::getEntityTypeMappings()

http://drupal.org/node/737726

File

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

Code

function token_entity_type_alter(array &$entity_types) {
  $devel_exists = \Drupal::moduleHandler()
    ->moduleExists('devel');

  /* @var $entity_types EntityTypeInterface[] */
  foreach ($entity_types as $entity_type_id => $entity_type) {
    if (!$entity_type
      ->get('token_type')) {

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

          // Stupid taxonomy token types...
          $entity_type
            ->set('token_type', str_replace('taxonomy_', '', $entity_type_id));
          break;
        default:

          // By default the token type is the same as the entity type.
          $entity_type
            ->set('token_type', $entity_type_id);
          break;
      }
    }
    if ($devel_exists && $entity_type
      ->hasViewBuilderClass() && ($canonical = $entity_type
      ->getLinkTemplate('canonical')) && !$entity_type
      ->hasLinkTemplate('token-devel')) {
      $entity_type
        ->setLinkTemplate('token-devel', $canonical . '/devel/token');
    }
  }
}