1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * Hooks provided by the token module. |
---|
6 | */ |
---|
7 | |
---|
8 | /** |
---|
9 | * @addtogroup hooks |
---|
10 | * @{ |
---|
11 | */ |
---|
12 | |
---|
13 | /** |
---|
14 | * Provide metadata about available placeholder tokens and token types. |
---|
15 | * |
---|
16 | * @param $type |
---|
17 | * The type of tokens to list (e.g. 'global', 'node', or 'user'). To list all |
---|
18 | * tokens, use 'all'. |
---|
19 | * |
---|
20 | * @return |
---|
21 | * An associative array of available tokens. The base array is keys of token |
---|
22 | * types and an array of its tokens. The token arrays are keys containing the |
---|
23 | * raw name of the token and values containing its user-friendly name. |
---|
24 | */ |
---|
25 | function hook_token_list($type = 'all') { |
---|
26 | $tokens = array(); |
---|
27 | |
---|
28 | if ($type == 'global' || $type == 'all') { |
---|
29 | $tokens['global']['random-number'] = t('A randomly generated number.'); |
---|
30 | |
---|
31 | } |
---|
32 | if ($type == 'node' || $type == 'all') { |
---|
33 | $tokens['node']['node-random-nid'] = t("A randomly generated number between one and the node's unique ID."); |
---|
34 | } |
---|
35 | |
---|
36 | return $tokens; |
---|
37 | } |
---|
38 | |
---|
39 | /** |
---|
40 | * Provide replacement values for placeholder tokens. |
---|
41 | * |
---|
42 | * @param $type |
---|
43 | * The type of token being replaced (e.g. 'global', 'node', or 'user'). |
---|
44 | * @param $data |
---|
45 | * (optional) An object to be used when generating replacement values. |
---|
46 | * @param $options |
---|
47 | * (optional) A associative array of options to control the token replacement |
---|
48 | * process. |
---|
49 | * |
---|
50 | * @return |
---|
51 | * An associative array of replacement values, keyed by the original 'raw' |
---|
52 | * tokens that were found in the source text. For example: |
---|
53 | * $values['title-raw'] = 'My new node'; |
---|
54 | * |
---|
55 | * @see hook_token_values_alter() |
---|
56 | */ |
---|
57 | function hook_token_values($type, $object = NULL, $options = array()) { |
---|
58 | $values = array(); |
---|
59 | |
---|
60 | if ($type == 'global') { |
---|
61 | $values['random-number'] = mt_rand(); |
---|
62 | } |
---|
63 | |
---|
64 | if ($type == 'node' && !empty($object)) { |
---|
65 | $values['node-random-nid'] = mt_rand(1, $object->nid); |
---|
66 | } |
---|
67 | |
---|
68 | return $values; |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * Alter replacement values for placeholder tokens. |
---|
73 | * |
---|
74 | * @param $replacements |
---|
75 | * An associative array of replacements returned by hook_token_values(). |
---|
76 | * @param $context |
---|
77 | * The context in which hook_token_values() was called. An associative array |
---|
78 | * with the following keys, which have the same meaning as the corresponding |
---|
79 | * parameters of hook_token_values(): |
---|
80 | * - 'type' |
---|
81 | * - 'object' |
---|
82 | * - 'options' |
---|
83 | * |
---|
84 | * @see hook_token_values() |
---|
85 | */ |
---|
86 | function hook_token_values_alter(&$replacements, $context) { |
---|
87 | if ($context['type'] == 'node' && !empty($context['object'])) { |
---|
88 | $node = $context['object']; |
---|
89 | |
---|
90 | if (isset($replacements['title-raw']) && !empty($node->field_title[0]['value'])) { |
---|
91 | $title = $node->field_title[0]['value']; |
---|
92 | $replacements['title-raw'] = $title; |
---|
93 | $replacements['title'] = check_plain($title); |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * @} End of "addtogroup hooks". |
---|
100 | */ |
---|