Drupal Token Examples of Usage
I need to include a node content in another node using some kind of placeholder, for example: [node-5663]
will be translated to the node's content (body) where node-id matches 5663.
The example above is just an example, what I would need is actually something like this: [table-TABLE-ID]
where TABLE-ID
will be a field I define in the node (using CCK).
I don't have any problem searching and开发者_StackOverflow社区 matching the content I need to fetch, but what I am missing is how to use the tokens.
Any help would be welcome :)
While I'm a little fuzzy on the exact details of what you want, the basic premise is actually quite simple.
You will want to build a custom module, which simply defines some tokens, similar to the following:
/**
* Implements hook_theme().
*/
function my_module_theme() {
return array(
'my_module' => array(
'arguments' => array('object' => NULL)
),
);
}
/**
* Implements hook_token_list().
*/
function my_module_token_list($type = 'all') {
if ($type == 'node' || $type == 'all') {
$tokens = array();
$tokens['my_module']['table-TABLE-ID'] = t('description').
return $tokens;
}
}
/**
* Implements hook_token_values().
*/
function my_module_token_values($type, $object = NULL) {
if ($type == 'node') {
($table, $id) = explode('-', $object->my_field['value']);
$tokens['table-' . $object->my_field['value']] = theme('my_module', db_fetch_object(db_query("SELECT * FROM {" . $table . "} WHERE id = %d", $id)));
return $tokens;
}
}
function theme_my_module($object) {
return '<div>' . $object->content . '</div>';
}
Note: All this code is theoretical and I can pretty much state that it will not work. It is also highly insecure to do the db_query the way I have done it here (which was my interpretation of what you wanted), instead you should have a token for each different type of query you want ('table-node-ID', etc).
Hope this is somewhat helpful.
If you need to access tokens from another module within php, a quick way to do that is with the drupal function "token_replace($text)"
You pass it text that may contain the token and it will return text with the token replaced.
example for Drupal 7
<?php
$tokentext = "I'm the ga_tokenizer:ga-term [ga_tokenizer:ga-term]";
$processedText = token_replace($tokentext);
print $processedText;
?>
This will output I'm the ga_tokenizer:ga-term THE SEARCH TERM USED TO FIND THE PAGE
If you just want the actual token value, use
<?php
$tokentext = "[ga_tokenizer:ga-term]";
$processedText = token_replace($tokentext);
?>
精彩评论