revisioning_tokens.inc in Revisioning 8
Same filename and directory in other branches
Add tokens pertaining to the Revisioning module.
File
revisioning_tokens.incView source
<?php
/**
* @file
* Add tokens pertaining to the Revisioning module.
*
* @ingroup token
*/
/**
* Implements hook_token_info().
*/
function revisioning_token_info() {
$info = array();
// First specify token type(s).
$info['types']['revision'] = array(
'name' => t("Revisions"),
'description' => t('Tokens related to revisions of individual content items, or "node revisions".'),
'needs-data' => 'node',
);
// Then specify the tokens.
$info['tokens']['revision']['revision-author'] = array(
'name' => t("Revision author"),
'description' => t("The author (or editor) of the revision"),
'type' => 'user',
);
$info['tokens']['revision']['revision-body'] = array(
'name' => t("Revision body"),
'description' => t("The main body text of the revision"),
);
$info['tokens']['revision']['revision-created'] = array(
'name' => t("Revision timestamp"),
'description' => t("The date and time the revision was created."),
'type' => 'date',
);
$info['tokens']['revision']['revision-title'] = array(
'name' => t("Revision title"),
'description' => t("The title of the revision"),
);
$info['tokens']['revision']['revision-vid'] = array(
'name' => t("Revision ID"),
'description' => t("The unique ID of the revision"),
);
return $info;
}
/**
* Implements hook_tokens().
*/
function revisioning_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if (!empty($data['revision'])) {
$node = $data['revision'];
}
elseif (!empty($data['entity']) && $data['entity_type'] == 'node') {
// When Token module is enabled.
$node = $data['entity'];
}
else {
return $replacements;
}
// This may not be necessary in all cases, but node data is cached anyway.
$revision = node_load($node->nid, $node->vid);
$sanitize = !empty($options['sanitize']);
foreach ($tokens as $name => $original) {
switch ($name) {
case 'revision-title':
$title = $revision->title;
$replacements[$original] = $sanitize ? check_plain($title) : $title;
break;
case 'revision-body':
$text = $revision->body[$revision->language][0]['value'];
$replacements[$original] = $sanitize ? check_plain($text) : $text;
break;
case 'revision-vid':
$replacements[$original] = $revision->vid;
break;
// Default values for the chained tokens handled below.
case 'revision-author':
$author = user_load($revision->uid);
$name = format_username($author);
$replacements[$original] = $sanitize ? check_plain($name) : $name;
break;
case 'revision-created':
$langcode = empty($options['language']->language) ? LANGUAGE_NONE : $options['language']->language;
$replacements[$original] = format_date($revision->created, 'medium', '', NULL, $langcode);
break;
}
}
// Chained tokens for revision author and revision timestamp.
// These fan out into sub-token fields, e.g revision-author:mail etc.
if ($author_tokens = token_find_with_prefix($tokens, 'revision-author')) {
$author = user_load($revision->uid);
$replacements += token_generate('user', $author_tokens, array(
'user' => $author,
), $options);
}
if ($created_tokens = token_find_with_prefix($tokens, 'revision-created')) {
$replacements += token_generate('date', $created_tokens, array(
'date' => $revision->revision_timestamp,
), $options);
}
return $replacements;
}
Functions
Name | Description |
---|---|
revisioning_tokens | Implements hook_tokens(). |
revisioning_token_info | Implements hook_token_info(). |