You are here

function revisioning_tokens in Revisioning 7

Same name and namespace in other branches
  1. 8 revisioning_tokens.inc \revisioning_tokens()

Implements hook_tokens().

1 string reference to 'revisioning_tokens'
revisioning.module in ./revisioning.module
Allows content to be updated and reviewed before submitting it for publication, while the current live revision remains unchanged and publicly visible until the changes have been reviewed and found fit for publication by a moderator.

File

./revisioning_tokens.inc, line 50
Add tokens pertaining to the Revisioning module.

Code

function revisioning_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  if (!empty($data['revision'])) {
    $revision = $data['revision'];
  }
  elseif (!empty($data['entity']) && $data['entity_type'] == 'node') {

    // When Token module is enabled.
    $revision = $data['entity'];
  }
  elseif (!empty($data['node'])) {
    $revision = $data['node'];
  }
  else {
    return $replacements;
  }
  $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 = reset($revision->body);
        $text = $text[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->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->revision_timestamp, '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->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;
}