You are here

public function Token::getInvalidTokensByContext in Token 8

Validate tokens in raw text based on possible contexts.

Parameters

string|string[] $value: A string with the raw text containing the raw tokens, or an array of tokens from token_scan().

string[] $valid_types: An array of token types that will be used when token replacement is performed.

Return value

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

Overrides TokenInterface::getInvalidTokensByContext

File

src/Token.php, line 177

Class

Token
Service to retrieve token information.

Namespace

Drupal\token

Code

public function getInvalidTokensByContext($value, array $valid_types = []) {
  if (in_array('all', $valid_types)) {
    $info = $this
      ->getInfo();
    $valid_types = array_keys($info['types']);
  }
  else {

    // Add the token types that are always valid in global context.
    $valid_types = array_merge($valid_types, $this
      ->getGlobalTokenTypes());
  }
  $invalid_tokens = [];
  $value_tokens = is_string($value) ? $this
    ->scan($value) : $value;
  foreach ($value_tokens as $type => $tokens) {
    if (!in_array($type, $valid_types)) {

      // If the token type is not a valid context, its tokens are invalid.
      $invalid_tokens = array_merge($invalid_tokens, array_values($tokens));
    }
    else {

      // Check each individual token for validity.
      $invalid_tokens = array_merge($invalid_tokens, $this
        ->getInvalidTokens($type, $tokens));
    }
  }
  array_unique($invalid_tokens);
  return $invalid_tokens;
}