You are here

function token_get_invalid_tokens_by_context in Token 7

Same name and namespace in other branches
  1. 5 token.module \token_get_invalid_tokens_by_context()
  2. 6 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().

$tokens: An array of token types that will be used when token replacement is performed.

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 619
Enhances the token API in core: adds a browseable UI, missing tokens, etc.

Code

function token_get_invalid_tokens_by_context($value, $valid_types = array()) {
  if (in_array('all', $valid_types)) {
    $info = token_get_info();
    $valid_types = array_keys($info['types']);
  }
  else {

    // Add the token types that are always valid in global context.
    $valid_types = array_merge($valid_types, token_get_global_token_types());
  }
  $invalid_tokens = array();
  $value_tokens = is_string($value) ? token_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, token_get_invalid_tokens($type, $tokens));
    }
  }
  array_unique($invalid_tokens);
  return $invalid_tokens;
}