You are here

function token_get_invalid_tokens_by_context in Token 6

Same name and namespace in other branches
  1. 5 token.module \token_get_invalid_tokens_by_context()
  2. 7 token.module \token_get_invalid_tokens_by_context()

Validate an tokens in raw text based on possible contexts.

Parameters

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

$valid_types: An array of token types to validage against.

$leading: Character(s) to prepend to the token key before searching for matches. Defaults to TOKEN_PREFIX.

$trailing: Character(s) to append to the token key before searching for matches. Defaults to TOKEN_SUFFIX.

Return value

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

2 calls to token_get_invalid_tokens_by_context()
TokenUnitTestCase::testGetInvalidTokens in ./token.test
Test token_get_invalid_tokens() and token_get_invalid_tokens_by_context().
token_element_validate in ./token.module
Validate a form element that should have tokens in it.

File

./token.module, line 646
The Token API module.

Code

function token_get_invalid_tokens_by_context($value, $valid_types = array(), $leading = TOKEN_PREFIX, $trailing = TOKEN_SUFFIX) {
  if (in_array('all', $valid_types)) {
    $valid_types = array(
      'all',
    );
  }
  else {

    // Add the token types that are always valid in global context.
    $valid_types[] = 'global';
  }
  $invalid_tokens = array();
  $valid_tokens = array();
  $value_tokens = is_string($value) ? token_scan($value, $leading, $trailing) : $value;
  foreach (token_get_list($valid_types) as $category => $tokens) {
    $valid_tokens += $tokens;
  }
  foreach ($value_tokens as $token) {
    if (isset($valid_tokens[$token])) {
      continue;
    }
    elseif (preg_match('/^(.*[_-])([^-_])+$/', $token, $matches)) {

      // Allow tokens that do not have a direct match to tokens listed in
      // hook_token_info() to be matched against a 'wildcard' token name.
      if (isset($valid_tokens[$matches[1] . '?'])) {

        // [token-name-?] wildcards.
        continue;
      }
      elseif (isset($valid_tokens[$matches[1] . '????'])) {

        // [token-name-????] wildcards.
        continue;
      }
      elseif (is_numeric($matches[2]) && isset($valid_tokens[$matches[1] . 'N'])) {

        // [token-name-N] wildcards if N is a numeric value.
        continue;
      }
    }
    $invalid_tokens[] = $token;
  }
  array_unique($invalid_tokens);
  $invalid_tokens = token_prepare_tokens($invalid_tokens, $leading, $trailing);
  return $invalid_tokens;
}