You are here

function token_get_invalid_tokens in Token 7

Validate an array of tokens based on their token type.

Parameters

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

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

Return value

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

1 call to token_get_invalid_tokens()
token_get_invalid_tokens_by_context in ./token.module
Validate an tokens in raw text based on possible contexts.

File

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

Code

function token_get_invalid_tokens($type, $tokens) {
  $token_info = token_get_info();
  $invalid_tokens = array();
  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 {

        // Resursively check the chained tokens.
        $sub_tokens = token_find_with_prefix(array(
          $token => $full_token,
        ), $parts[0]);
        $invalid_tokens = array_merge($invalid_tokens, token_get_invalid_tokens($sub_token_info['type'], $sub_tokens));
      }
    }
  }
  return $invalid_tokens;
}