You are here

function token_get_invalid_tokens_by_context in Token 5

Same name and namespace in other branches
  1. 6 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

$text: A string with the raw text containing the raw tokens.

$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 an open-bracket.

trailing: Character(s) to append to the token key before searching for matches. Defaults to a close-bracket.

Return value

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

1 call to token_get_invalid_tokens_by_context()
token_element_validate_token_context in ./token.module
Validate a form element that should have tokens in it.

File

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

Code

function token_get_invalid_tokens_by_context($text, $valid_types = array(), $leading = TOKEN_PREFIX, $trailing = TOKEN_SUFFIX) {

  // Add the token types that are always valid in global context.
  $valid_types[] = 'global';
  $invalid_tokens = array();
  $valid_tokens = token_get_list($valid_types);
  foreach (token_scan($text, $leading, $trailing) as $token) {
    $found = FALSE;
    foreach ($valid_tokens as $category => $tokens) {
      if (isset($tokens[$token])) {
        $found = TRUE;
        break;
      }
      elseif (preg_match('/^(.*[_-])\\d+$/', $token, $result)) {

        // Some modules use a 'wildcard' token, e.g. Location.module defines
        // [location-country_N] but users actually use [location-country_0].
        // If the token has a digit on the end and there is a 'wildcard'
        // token definition, we should allow it.
        if (isset($tokens[$result[1] . 'N']) || isset($tokens[$result[1] . '?'])) {
          $found = TRUE;
          break;
        }
      }
    }
    if (!$found) {
      $invalid_tokens[] = $token;
    }
  }
  array_unique($invalid_tokens);
  $invalid_tokens = token_prepare_tokens($invalid_tokens, $leading, $trailing);
  return $invalid_tokens;
}