169
* Return the value of $original, with all instances of placeholder
170
* tokens replaced by their proper values. To replace multiple types
171
* at once see token_replace_multiple().
174
* A string, or an array of strings, to perform token substitutions
177
* A flag indicating the class of substitution tokens to use. If an
178
* object is passed in the second param, 'type' should contain the
179
* object's type. For example, 'node', 'comment', or 'user'. If no
180
* type is specified, only 'global' site-wide substitution tokens are
183
* Optionally, the object to use for building substitution values.
184
* A node, comment, user, etc.
186
* Character(s) to prepend to the token key before searching for
187
* matches. Defaults to an open-bracket.
189
* Character(s) to append to the token key before searching for
190
* matches. Defaults to a close-bracket.
192
* A flag indicating whether or not to flush the token cache.
193
* Useful for processes that need to slog through huge numbers
194
* of tokens in a single execution cycle. Flushing it will
195
* keep them from burning through memory.
196
* The default is FALSE.
197
* @return The modified version of $original, with all substitutions
181
* Replace all tokens in a given string with appropriate values.
184
* A string potentially containing replaceable tokens.
186
* (optional) A flag indicating the class of substitution tokens to use. If
187
* an object is passed in the second param, 'type' should contain the
188
* object's type. For example, 'node', 'comment', or 'user'. If no type is
189
* specified, only 'global' site-wide substitution tokens are built.
191
* (optional) An object to use for building substitution values (e.g. a node
192
* comment, or user object).
194
* (optional) Character(s) to prepend to the token key before searching for
195
* matches. Defaults to TOKEN_PREFIX.
197
* (optional) Character(s) to append to the token key before searching for
198
* matches. Defaults to TOKEN_SUFFIX.
200
* (optional) A keyed array of settings and flags to control the token
201
* generation and replacement process.
203
* (optional) A flag indicating whether or not to flush the token cache.
204
* Useful for processes that need to slog through huge numbers of tokens
205
* in a single execution cycle. Flushing it will keep them from burning
206
* through memory. Defaults to FALSE.
209
* Text with tokens replaced.
200
function token_replace($original, $type = 'global', $object = NULL, $leading = TOKEN_PREFIX, $trailing = TOKEN_SUFFIX, $options = array(), $flush = FALSE) {
211
function token_replace($text, $type = 'global', $object = NULL, $leading = TOKEN_PREFIX, $trailing = TOKEN_SUFFIX, $options = array(), $flush = FALSE) {
201
212
$full = token_get_values($type, $object, $flush, $options);
202
return _token_replace_tokens($original, $full->tokens, $full->values, $leading, $trailing);
213
$tokens = token_prepare_tokens($full->tokens, $leading, $trailing);
214
return str_replace($tokens, $full->values, $text);
206
* Return the value of $original, with all instances of placeholder
207
* tokens replaced by their proper values. Contrary to token_replace(),
208
* this function supports replacing multiple types.
211
* A string, or an array of strings, to perform token substitutions
214
* An array of substitution classes and optional objects. The key is
215
* a flag indicating the class of substitution tokens to use.
216
* If an object is passed as value, the key should contain the
217
* object's type. For example, 'node', 'comment', or 'user'. The
218
* object will be used for building substitution values. If no type
219
* is specified, only 'global' site-wide substitution tokens are built.
221
* Character(s) to prepend to the token key before searching for
222
* matches. Defaults to an open-bracket.
224
* Character(s) to append to the token key before searching for
225
* matches. Defaults to a close-bracket.
227
* A flag indicating whether or not to flush the token cache.
228
* Useful for processes that need to slog through huge numbers
229
* of tokens in a single execution cycle. Flushing it will
230
* keep them from burning through memory.
231
* The default is FALSE.
232
* @return The modified version of $original, with all substitutions
218
* Replace all tokens in a given string with appropriate values.
220
* Contrary to token_replace() this function supports replacing multiple types.
223
* A string potentially containing replaceable tokens.
225
* (optional) An array of substitution classes and optional objects. The key
226
* is a flag indicating the class of substitution tokens to use. If an object
227
* is passed as value, the key should contain the object's type. For example,
228
* 'node', 'comment', or 'user'. The object will be used for building
229
* substitution values. If no type is specified, only 'global' site-wide
230
* substitution tokens are built.
232
* (optional) Character(s) to prepend to the token key before searching for
233
* matches. Defaults to TOKEN_PREFIX.
235
* (optional) Character(s) to append to the token key before searching for
236
* matches. Defaults to TOKEN_SUFFIX.
238
* (optional) A keyed array of settings and flags to control the token
239
* generation and replacement process.
241
* (optional) A flag indicating whether or not to flush the token cache.
242
* Useful for processes that need to slog through huge numbers of tokens
243
* in a single execution cycle. Flushing it will keep them from burning
244
* through memory. Defaults to FALSE.
247
* Text with tokens replaced.
235
function token_replace_multiple($original, $types = array('global' => NULL), $leading = TOKEN_PREFIX, $trailing = TOKEN_SUFFIX, $options = array(), $flush = FALSE) {
249
function token_replace_multiple($text, $types = array(), $leading = TOKEN_PREFIX, $trailing = TOKEN_SUFFIX, $options = array(), $flush = FALSE) {
236
250
$full = new stdClass();
237
251
$full->tokens = $full->values = array();
253
// Allow global token replacement by default.
254
if (empty($types) || !is_array($types)) {
255
$types = array('global' => NULL);
238
258
foreach ($types as $type => $object) {
239
259
$temp = token_get_values($type, $object, $flush, $options);
240
260
$full->tokens = array_merge($full->tokens, $temp->tokens);
241
261
$full->values = array_merge($full->values, $temp->values);
243
return _token_replace_tokens($original, $full->tokens, $full->values, $leading, $trailing);
246
// Internally used function to replace tokens.
247
function _token_replace_tokens($original, $tokens, $values, $leading, $trailing) {
248
$tokens = token_prepare_tokens($tokens, $leading, $trailing);
249
return str_replace($tokens, $values, $original);
264
$tokens = token_prepare_tokens($full->tokens, $leading, $trailing);
265
return str_replace($tokens, $full->values, $text);
253
269
* Return a list of valid substitution tokens and their values for
254
270
* the specified type.
257
* A flag indicating the class of substitution tokens to use. If an
273
* (optional) A flag indicating the class of substitution tokens to use. If an
258
274
* object is passed in the second param, 'type' should contain the
259
275
* object's type. For example, 'node', 'comment', or 'user'. If no
260
276
* type is specified, only 'global' site-wide substitution tokens are
263
* Optionally, the object to use for building substitution values.
264
* A node, comment, user, etc.
266
* A flag indicating whether or not to flush the token cache.
267
* Useful for processes that need to slog through huge numbers
268
* of tokens in a single execution cycle. Flushing it will
269
* keep them from burning through memory.
270
* The default is FALSE.
279
* (optional) An object to use for building substitution values (e.g. a node
280
* comment, or user object).
282
* (optional) A flag indicating whether or not to flush the token cache.
283
* Useful for processes that need to slog through huge numbers of tokens
284
* in a single execution cycle. Flushing it will keep them from burning
285
* through memory. Defaults to FALSE.
287
* (optional) A keyed array of settings and flags to control the token
288
* generation process.
272
* A keyed array containing the substitution tokens and the substitution
273
* values for the passed-in type and object.
291
* An object with two properties:
292
* - tokens: All the possible tokens names generated.
293
* - values: The corresponding values for the tokens.
295
* Note that before performing actual token replacement that the token names
296
* should be run through token_prepare_tokens().
275
298
function token_get_values($type = 'global', $object = NULL, $flush = FALSE, $options = array()) {
335
* Merge two arrays of token values, checking and warning for duplication.
337
* Because array_merge will combinue elements that have the same key into an
338
* array instead of overriding the existing element, this causes problems since
339
* we need the values to always be a string.
342
* An array of token values keyed by token name.
344
* An array of token values keyed by token name.
346
* A merged array of token values keyed by token name.
348
function _token_array_merge($array1, $array2) {
349
static $warned = array();
351
$merged = array_merge($array1, $array2);
352
foreach ($merged as $key => $value) {
353
if (is_array($value)) {
354
if (empty($warned[$key])) {
355
// Only warn once about duplicate token.
356
watchdog('token', 'More than one module has defined the %key token.', array('%key' => $key), WATCHDOG_WARNING);
357
$warned[$key] = TRUE;
359
// Use the last-defined value (module with lowest weight).
360
$merged[$key] = array_pop($value);
368
358
* A helper function that retrieves all currently exposed tokens,
369
359
* and merges them recursively. This is only necessary when building
370
360
* the token listing -- during actual value replacement, only tokens
371
361
* in a particular domain are requested and a normal array_marge() is
375
365
* A flag indicating the class of substitution tokens to use. If an
376
366
* object is passed in the second param, 'types' should contain the
377
367
* object's type. For example, 'node', 'comment', or 'user'. 'types'
378
368
* may also be an array of types of the form array('node','user'). If no
379
369
* type is specified, only 'global' site-wide substitution tokens are
382
373
* The array of usable tokens and their descriptions, organized by