You are here

function Token::getInvalidTokens in Token 8

Validate an array of tokens based on their token type.

Parameters

string $type: The type of tokens to validate (e.g. 'node', etc.)

string[] $tokens: A keyed array of tokens, and their original raw form in the source text.

Return value

string[] An array with the invalid tokens in their original raw forms.

Overrides TokenInterface::getInvalidTokens

1 call to Token::getInvalidTokens()
Token::getInvalidTokensByContext in src/Token.php
Validate tokens in raw text based on possible contexts.

File

src/Token.php, line 137

Class

Token
Service to retrieve token information.

Namespace

Drupal\token

Code

function getInvalidTokens($type, $tokens) {
  $token_info = $this
    ->getInfo();
  $invalid_tokens = [];
  foreach ($tokens as $token => $full_token) {
    if (isset($token_info['tokens'][$type][$token])) {
      continue;
    }

    // Split token up if it has chains.
    $parts = explode(':', $token, 2);
    if (!isset($token_info['tokens'][$type][$parts[0]])) {

      // This is an invalid token (not defined).
      $invalid_tokens[] = $full_token;
    }
    elseif (count($parts) == 2) {
      $sub_token_info = $token_info['tokens'][$type][$parts[0]];
      if (!empty($sub_token_info['dynamic'])) {

        // If this token has been flagged as a dynamic token, skip it.
        continue;
      }
      elseif (empty($sub_token_info['type'])) {

        // If the token has chains, but does not support it, it is invalid.
        $invalid_tokens[] = $full_token;
      }
      else {

        // Recursively check the chained tokens.
        $sub_tokens = $this
          ->findWithPrefix([
          $token => $full_token,
        ], $parts[0]);
        $invalid_tokens = array_merge($invalid_tokens, $this
          ->getInvalidTokens($sub_token_info['type'], $sub_tokens));
      }
    }
  }
  return $invalid_tokens;
}