source: sipes/modules_contrib/token/API.txt @ c43ea01

stableversion-3.0
Last change on this file since c43ea01 was 177a560, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se agrego el directorio de modulos contribuidos de drupal

  • Propiedad mode establecida a 100755
File size: 5.1 KB
Línea 
1
2Overview
3========
4In many cases, it's useful to allow users to define patterns or large
5chunks of text that contain programmatically derived values. For example,
6form email messages addressed to a given user, or url path aliases
7containing the title of a given node. Both examples require bits of data
8that vary each time the text is generated -- node titles, user ids, and
9so on. Rather than forcing users to embed ugly snippets of PHP, or creating
10elaborate and bizarre UIs for configuring the patterns via the browser,
11it's most useful to give users a set of 'placeholder' tokens to place in
12their text.
13
14Token.module provides a shared API for exposing and using placeholder
15tokens and their appropriate replacement values. It does nothing *by
16itself* -- other modules can use it to avoid reinventing the wheel.
17
18Using Token Replacement
19=======================
20To apply token replacement to a chunk of text, you have two options. The
21first, and simplest, is:
22
23  token_replace($original, $type = 'global', $object = NULL,
24                $leading = '[', $trailing = ']')
25
26$original is the source text to perform substitutions on: it can be either
27a simple string, or an array of multiple strings.
28
29$type and $object are to be used when performing substitution based on a
30particular Drupal object. Replacing tokens in an email with values from
31a particular user account, or replacing tokens in a path alias pattern with
32values from the node being aliased, are two examples.
33
34$type should contain the general object type (node, comment, user, etc.)
35while $object should contain the object itself.
36
37$leading and $trailing can be used to override the default token style.
38For example, to replace tokens using %this style, pass in '%' and '' for
39the $leading and $trailing values. Note that passing in a leading but NOT
40trailing value can lead to false positives if two tokens are named in a
41similar fashion (%node_term and %node_term_id, for example).
42
43
44
45Altering The Replacement Values
46===============================
47If your module needs to perform additional cleanup work on the replacement
48values before doing the actual substitutions (cleaning replacement values
49to make them appropriate for path aliasing, truncating them to a particular
50length, etc.) you can manually retrieve the list of tokens and replacement
51values, then call str_replace() yourself.
52
53  token_get_values($type = 'global', $object = NULL)
54
55Pass in the $type and $object as you would with the simpler token_replace()
56function. The return value will be an object containing one array of tokens
57and one array of values as in this example:
58
59stdClass Object {
60  [tokens] => array(
61    [0] => mytoken1,
62    [1] => mytoken2
63  ),
64  [values] => array(
65    [0] => value1,
66    [1] => value2,
67  )
68}
69
70
71
72Providing Placeholder Tokens
73============================
74Token.module provides a small set of default placeholders for global values
75like the name of the currently logged in user, the site's URL, and so on.
76Any module can provide additional tokens by implementing two hooks.
77
78Security note: For tokens which include user input, users and modules
79expect to see both a ['token-name'] and a ['token-name-raw'] value.
80
81
82hook_token_values($type, $object = NULL)
83========================================
84This function should return a keyed array of placeholders, and their
85replacement values. $type contains the current context -- 'node', 'user',
86'global', etc. $object contains the specific node, user, etc. that
87should be used as the basis for the replacements. *Only* generate and
88return replacement tokens when $type is something that your module can
89really deal with. That helps keep things speedy and avoid needlessly
90searching for jillions of replacement tokens. The $options array can
91contain additional options (exact use is dynamic and not easily documented).
92
93For example:
94
95function my_user_token_values($type, $object = NULL, $options = array()) {
96  if ($type == 'user') {
97    $user = $object;
98    $tokens['name']      = $user->name;
99    $tokens['mail']      = $user->mail;
100    return $tokens;
101  }
102}
103
104
105hook_token_list($type = 'all')
106==============================
107This function is used to provide help and inline documentation for all
108of the possible replacement tokens.
109
110As with hook_token_values, $type indicates the context that token help
111is being generated for. Unlike hook_token_values however, you should
112show ALL tokens at the same time if $type is 'all'. As such, the help
113text should be keyed by the $type context your module will use when
114doing the actual replacements. For example:
115
116function my_user_token_list($type = 'all') {
117  if ($type == 'user' || $type == 'all') {
118    $tokens['user']['name']      = t("The user's name");
119    $tokens['user']['mail']      = t("The user's email address");
120    return $tokens;
121  }
122}
123
124Examples of more elaborate token replacement setups can be found in the
125token_node.inc file that's bundled with token.module.
126
127Security Note
128========
129If  use any of the tokens in the ['raw'] sub-array then please note that these
130are unfiltered values which could conceivably contain XSS attacks or other
131malicious data.  Your module should then provide it's own filtering to ensure the
132safety of site users.
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.