summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--includes/token.inc19
-rw-r--r--modules/system/system.api.php97
2 files changed, 82 insertions, 34 deletions
diff --git a/includes/token.inc b/includes/token.inc
index 2db6c78c9..57c15ea4f 100644
--- a/includes/token.inc
+++ b/includes/token.inc
@@ -152,17 +152,24 @@ function token_scan($text) {
* An associative array of replacement values, keyed by the original 'raw'
* tokens that were found in the source text. For example:
* $results['[node:title]'] = 'My new node';
+ *
+ * @see hook_tokens()
+ * @see hook_tokens_alter()
*/
function token_generate($type, array $tokens, array $data = array(), array $options = array()) {
- $results = array();
$options += array('sanitize' => TRUE);
+ $replacements = module_invoke_all('tokens', $type, $tokens, $data, $options);
- $result = module_invoke_all('tokens', $type, $tokens, $data, $options);
- foreach ($result as $original => $replacement) {
- $results[$original] = $replacement;
- }
+ // Allow other modules to alter the replacements.
+ $context = array(
+ 'type' => $type,
+ 'tokens' => $tokens,
+ 'data' => $data,
+ 'options' => $options,
+ );
+ drupal_alter('tokens', $replacements, $context);
- return $results;
+ return $replacements;
}
/**
diff --git a/modules/system/system.api.php b/modules/system/system.api.php
index 059bafcd6..d26548131 100644
--- a/modules/system/system.api.php
+++ b/modules/system/system.api.php
@@ -3807,6 +3807,8 @@ function hook_username_alter(&$name, $account) {
* An associative array of replacement values, keyed by the original 'raw'
* tokens that were found in the source text. For example:
* $results['[node:title]'] = 'My new node';
+ *
+ * @see hook_tokens_alter()
*/
function hook_tokens($type, $tokens, array $data = array(), array $options = array()) {
$url_options = array('absolute' => TRUE);
@@ -3865,6 +3867,46 @@ function hook_tokens($type, $tokens, array $data = array(), array $options = arr
}
/**
+ * Alter replacement values for placeholder tokens.
+ *
+ * @param $replacements
+ * An associative array of replacements returned by hook_tokens().
+ * @param $context
+ * The context in which hook_tokens() was called. An associative array with
+ * the following keys, which have the same meaning as the corresponding
+ * parameters of hook_tokens():
+ * - 'type'
+ * - 'tokens'
+ * - 'data'
+ * - 'options'
+ *
+ * @see hook_tokens()
+ */
+function hook_tokens_alter(array &$replacements, array $context) {
+ $options = $context['options'];
+
+ if (isset($options['language'])) {
+ $url_options['language'] = $options['language'];
+ $language_code = $options['language']->language;
+ }
+ else {
+ $language_code = NULL;
+ }
+ $sanitize = !empty($options['sanitize']);
+
+ if ($context['type'] == 'node' && !empty($context['data']['node'])) {
+ $node = $context['data']['node'];
+
+ // Alter the [node:title] token, and replace it with the rendered content
+ // of a field (field_title).
+ if (isset($context['tokens']['title'])) {
+ $title = field_view_field('node', $node, 'field_title', 'default', $language_code);
+ $replacements[$context['tokens']['title']] = drupal_render($title);
+ }
+ }
+}
+
+/**
* Provide metadata about available placeholder tokens and token types.
*
* @return
@@ -3914,34 +3956,6 @@ function hook_token_info() {
}
/**
- * Alter batch information before a batch is processed.
- *
- * Called by batch_process() to allow modules to alter a batch before it is
- * processed.
- *
- * @param $batch
- * The associative array of batch information. See batch_set() for details on
- * what this could contain.
- *
- * @see batch_set()
- * @see batch_process()
- *
- * @ingroup batch
- */
-function hook_batch_alter(&$batch) {
- // If the current page request is inside the overlay, add ?render=overlay to
- // the success callback URL, so that it appears correctly within the overlay.
- if (overlay_get_mode() == 'child') {
- if (isset($batch['url_options']['query'])) {
- $batch['url_options']['query']['render'] = 'overlay';
- }
- else {
- $batch['url_options']['query'] = array('render' => 'overlay');
- }
- }
-}
-
-/**
* Alter the metadata about available placeholder tokens and token types.
*
* @param $data
@@ -3968,6 +3982,33 @@ function hook_token_info_alter(&$data) {
);
}
+/**
+ * Alter batch information before a batch is processed.
+ *
+ * Called by batch_process() to allow modules to alter a batch before it is
+ * processed.
+ *
+ * @param $batch
+ * The associative array of batch information. See batch_set() for details on
+ * what this could contain.
+ *
+ * @see batch_set()
+ * @see batch_process()
+ *
+ * @ingroup batch
+ */
+function hook_batch_alter(&$batch) {
+ // If the current page request is inside the overlay, add ?render=overlay to
+ // the success callback URL, so that it appears correctly within the overlay.
+ if (overlay_get_mode() == 'child') {
+ if (isset($batch['url_options']['query'])) {
+ $batch['url_options']['query']['render'] = 'overlay';
+ }
+ else {
+ $batch['url_options']['query'] = array('render' => 'overlay');
+ }
+ }
+}
/**
* Provide information on Updaters (classes that can update Drupal).