You are here

function aet_token_info in Advanced Entity Tokens 2.x

Same name and namespace in other branches
  1. 7 aet.tokens.inc \aet_token_info()

Implements hook_token_info().

Declaring the AET tokens.

File

./aet.module, line 17
The AET main module file.

Code

function aet_token_info() {

  // Initializing the $info array.
  $info = [
    'types' => [],
    'tokens' => [],
  ];
  $info['types']['aet'] = [];
  $info['types']['aet']['name'] = t('Advanced Entity Tokens');
  $info['types']['aet']['description'] = t('AET Description');

  // Getting the information arrays on all the entities.
  $entities = \Drupal::service('entity_type.manager')
    ->getDefinitions();

  // Looping the entity information array to add the token types.
  foreach ($entities as $entity_type => $entity_info) {

    /** @var \Drupal\Core\Entity\EntityType $entity_info */
    $token_type = $entity_info
      ->get('additional')['token_type'] ?: $entity_type;
    if (empty($info['types'])) {
      $info['types'] = [];
    }
    if (empty($info['tokens'])) {
      $info['tokens'] = [];
    }
    $entity_id_key = $entity_info
      ->getKey('id');

    // Adding the entity element token.
    // The token key consists of the entity key id and a question mark
    // to mark this as the place to enter the entity's id.
    $info['tokens']['aet'][$token_type] = [];
    $info['tokens']['aet'][$token_type]['name'] = 'AET ' . $entity_info
      ->getBundleLabel() . ' entity';
    $info['tokens']['aet'][$token_type]['description'] = t("Reference a @entity_name entity by using it's @id_key", [
      '@entity_name' => $entity_info
        ->getBundleLabel(),
      '@id_key' => $entity_id_key,
    ]);
    $info['tokens']['aet'][$token_type]['dynamic'] = TRUE;
    $info['tokens']['aet'][$token_type]['type'] = $token_type;
  }
  return $info;
}