You are here

public function Token::getInfo in Token 8

Returns metadata describing supported tokens.

The metadata array contains token type, name, and description data as well as an optional pointer indicating that the token chains to another set of tokens.

Return value

array An associative array of token information, grouped by token type. The array structure is identical to that of hook_token_info().

Overrides Token::getInfo

See also

hook_token_info()

5 calls to Token::getInfo()
Token::getGlobalTokenTypes in src/Token.php
Get a list of token types that can be used without any context (global).
Token::getInvalidTokens in src/Token.php
Validate an array of tokens based on their token type.
Token::getInvalidTokensByContext in src/Token.php
Validate tokens in raw text based on possible contexts.
Token::getTokenInfo in src/Token.php
Returns metadata describing supported a token.
Token::getTypeInfo in src/Token.php
Returns metadata describing supported token types.

File

src/Token.php, line 32

Class

Token
Service to retrieve token information.

Namespace

Drupal\token

Code

public function getInfo() {
  if (empty($this->tokenInfo)) {
    $cache_id = 'token_info_sorted:' . $this->languageManager
      ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
      ->getId();
    $cache = $this->cache
      ->get($cache_id);
    if ($cache) {
      $this->tokenInfo = $cache->data;
    }
    else {
      $token_info = $this->moduleHandler
        ->invokeAll('token_info');
      $this->moduleHandler
        ->alter('token_info', $token_info);
      foreach (array_keys($token_info['types']) as $type_key) {
        if (isset($token_info['types'][$type_key]['type'])) {
          $base_type = $token_info['types'][$type_key]['type'];

          // If this token type extends another token type, then merge in
          // the base token type's tokens.
          if (isset($token_info['tokens'][$base_type])) {
            $token_info['tokens'] += [
              $type_key => [],
            ];
            $token_info['tokens'][$type_key] += $token_info['tokens'][$base_type];
          }
        }
        else {

          // Add a 'type' value to each token type information.
          $token_info['types'][$type_key]['type'] = $type_key;
        }
      }

      // Pre-sort tokens.
      $by_name = $this
        ->prepareMultisort($token_info['types']);
      array_multisort($by_name, SORT_ASC, SORT_NATURAL | SORT_FLAG_CASE, $token_info['types']);
      foreach (array_keys($token_info['tokens']) as $type) {
        $by_name = $this
          ->prepareMultisort($token_info['tokens'][$type]);
        array_multisort($by_name, SORT_ASC, SORT_NATURAL | SORT_FLAG_CASE, $token_info['tokens'][$type]);
      }
      $this->tokenInfo = $token_info;
      $this->cache
        ->set($cache_id, $this->tokenInfo, CacheBackendInterface::CACHE_PERMANENT, [
        static::TOKEN_INFO_CACHE_TAG,
      ]);
    }
  }
  return $this->tokenInfo;
}