You are here

function revisioning_tokens in Revisioning 8

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

Implements hook_tokens().

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'])) {
    $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;
}