function api_tokens_render in API Tokens 7
Processes API tokens.
1 call to api_tokens_render()
- api_tokens_filter_tokens in ./
api_tokens.module - Filter process callback for the API Tokens input filter.
File
- ./
api_tokens.module, line 239 - The API Tokens module
Code
function api_tokens_render($text) {
static $parents = array();
static $reported = array();
// Finding tokens entries to process
// [api:token_key1[param1, param2, ...]/], [api:token_key2/].
$token_count = preg_match_all(API_TOKENS_PATTERN, $text, $matches);
// Some tokens were found.
if ($token_count) {
$replacements = $matches[0];
$keys = $matches[1];
$params = $matches[2];
// Receiving all registered tokens.
$tokens = api_tokens_collect_tokens();
// Array of rendered tokens.
$rendered = array();
// Array of token cache.
$cids = array();
// Loop through all the tokens.
for ($i = 0; $i < $token_count; ++$i) {
$keys[$i] = strtolower($keys[$i]);
// Checking if token is registered.
if (array_key_exists($keys[$i], $tokens)) {
$rendered[$i] = FALSE;
$token_info = $tokens[$keys[$i]];
// Parsing token parameters.
$params[$i] = $params[$i] ? drupal_json_decode($params[$i]) : array();
// Token is cacheable.
if (DRUPAL_NO_CACHE != $token_info['cache']) {
$cids[$i] = api_tokens_cache_id($token_info, $params[$i]);
}
}
else {
$rendered[$i] = '';
}
}
// There were cacheable tokens.
if ($cids) {
// We have collected all cids, so we can grab all necessary at once.
$cache = api_tokens_cache_get($cids);
foreach ($cache as $i => $data) {
$rendered[$i] = $data;
}
}
// Processing tokens with missing cache.
foreach ($rendered as $i => $was_rendered) {
$key = $keys[$i];
$param = $params[$i];
$token = $replacements[$i];
if (FALSE === $was_rendered) {
$token_info = $tokens[$key];
if (api_tokens_prepare_handler($key)) {
// Receiving number of required parameters.
$param_count = isset($token_info['params']) ? $token_info['params'] : api_tokens_param_info($key);
// We have enough parameters.
if ($param_count <= count($param)) {
if (in_array($token, $parents)) {
$rendered[$i] = '';
if (!isset($reported[$token])) {
$reported[$token] = TRUE;
drupal_set_message(t('API Tokens: Recursion detected while rendering %token token.', array(
'%token' => $token,
)), 'warning');
}
}
else {
array_push($parents, $token);
// Call to token process function.
$rendered[$i] = call_user_func_array($token_info['handler'], $param);
array_pop($parents);
}
// Caching handler result, if token is cacheable.
if (DRUPAL_NO_CACHE != $token_info['cache']) {
api_tokens_cache_set($cids[$i], $rendered[$i], $token_info);
}
}
}
}
$info = array(
'key' => $key,
'params' => $param,
'token' => $token,
'parents' => $parents,
);
drupal_alter(__FUNCTION__, $rendered[$i], $info);
}
// Performing replacements at once to avoid recursing.
$text = str_replace($replacements, $rendered, $text);
}
return $text;
}