Drupal PHP Cross Reference Content Management Systems

Source: /modules/comment/comment.tokens.inc - 243 lines - 7851 bytes - Summary - Text - Print

   1  <?php
   2  
   3  /**
   4   * @file
   5   * Builds placeholder replacement tokens for comment-related data.
   6   */
   7  
   8  /**
   9   * Implements hook_token_info().
  10   */
  11  function comment_token_info() {
  12    $type = array(
  13      'name' => t('Comments'),
  14      'description' => t('Tokens for comments posted on the site.'),
  15      'needs-data' => 'comment',
  16    );
  17  
  18    // Comment-related tokens for nodes
  19    $node['comment-count'] = array(
  20      'name' => t("Comment count"),
  21      'description' => t("The number of comments posted on a node."),
  22    );
  23    $node['comment-count-new'] = array(
  24      'name' => t("New comment count"),
  25      'description' => t("The number of comments posted on a node since the reader last viewed it."),
  26    );
  27  
  28    // Core comment tokens
  29    $comment['cid'] = array(
  30      'name' => t("Comment ID"),
  31      'description' => t("The unique ID of the comment."),
  32    );
  33    $comment['hostname'] = array(
  34      'name' => t("IP Address"),
  35      'description' => t("The IP address of the computer the comment was posted from."),
  36    );
  37    $comment['name'] = array(
  38      'name' => t("Name"),
  39      'description' => t("The name left by the comment author."),
  40    );
  41    $comment['mail'] = array(
  42      'name' => t("Email address"),
  43      'description' => t("The email address left by the comment author."),
  44    );
  45    $comment['homepage'] = array(
  46      'name' => t("Home page"),
  47      'description' => t("The home page URL left by the comment author."),
  48    );
  49    $comment['title'] = array(
  50      'name' => t("Title"),
  51      'description' => t("The title of the comment."),
  52    );
  53    $comment['body'] = array(
  54      'name' => t("Content"),
  55      'description' => t("The formatted content of the comment itself."),
  56    );
  57    $comment['url'] = array(
  58      'name' => t("URL"),
  59      'description' => t("The URL of the comment."),
  60    );
  61    $comment['edit-url'] = array(
  62      'name' => t("Edit URL"),
  63      'description' => t("The URL of the comment's edit page."),
  64    );
  65  
  66    // Chained tokens for comments
  67    $comment['created'] = array(
  68      'name' => t("Date created"),
  69      'description' => t("The date the comment was posted."),
  70      'type' => 'date',
  71    );
  72    $comment['changed'] = array(
  73      'name' => t("Date changed"),
  74      'description' => t("The date the comment was most recently updated."),
  75      'type' => 'date',
  76    );
  77    $comment['parent'] = array(
  78      'name' => t("Parent"),
  79      'description' => t("The comment's parent, if comment threading is active."),
  80      'type' => 'comment',
  81    );
  82    $comment['node'] = array(
  83      'name' => t("Node"),
  84      'description' => t("The node the comment was posted to."),
  85      'type' => 'node',
  86    );
  87    $comment['author'] = array(
  88      'name' => t("Author"),
  89      'description' => t("The author of the comment, if they were logged in."),
  90      'type' => 'user',
  91    );
  92  
  93    return array(
  94      'types' => array('comment' => $type),
  95      'tokens' => array(
  96        'node' => $node,
  97        'comment' => $comment,
  98      ),
  99    );
 100  }
 101  
 102  /**
 103   * Implements hook_tokens().
 104   */
 105  function comment_tokens($type, $tokens, array $data = array(), array $options = array()) {
 106    $url_options = array('absolute' => TRUE);
 107    if (isset($options['language'])) {
 108      $url_options['language'] = $options['language'];
 109      $language_code = $options['language']->language;
 110    }
 111    else {
 112      $language_code = NULL;
 113    }
 114    $sanitize = !empty($options['sanitize']);
 115  
 116    $replacements = array();
 117  
 118    if ($type == 'comment' && !empty($data['comment'])) {
 119      $comment = $data['comment'];
 120  
 121      foreach ($tokens as $name => $original) {
 122        switch ($name) {
 123          // Simple key values on the comment.
 124          case 'cid':
 125            $replacements[$original] = $comment->cid;
 126            break;
 127  
 128          // Poster identity information for comments
 129          case 'hostname':
 130            $replacements[$original] = $sanitize ? check_plain($comment->hostname) : $comment->hostname;
 131            break;
 132  
 133          case 'name':
 134            $name = ($comment->uid == 0) ? variable_get('anonymous', t('Anonymous')) : $comment->name;
 135            $replacements[$original] = $sanitize ? filter_xss($name) : $name;
 136            break;
 137  
 138          case 'mail':
 139            if ($comment->uid != 0) {
 140              $account = user_load($comment->uid);
 141              $mail = $account->mail;
 142            }
 143            else {
 144              $mail = $comment->mail;
 145            }
 146            $replacements[$original] = $sanitize ? check_plain($mail) : $mail;
 147            break;
 148  
 149          case 'homepage':
 150            $replacements[$original] = $sanitize ? check_url($comment->homepage) : $comment->homepage;
 151            break;
 152  
 153          case 'title':
 154            $replacements[$original] = $sanitize ? filter_xss($comment->subject) : $comment->subject;
 155            break;
 156  
 157          case 'body':
 158            if ($items = field_get_items('comment', $comment, 'comment_body', $language_code)) {
 159              $instance = field_info_instance('comment', 'body', 'comment_body');
 160              $field_langcode = field_language('comment', $comment, 'comment_body', $language_code);
 161              $replacements[$original] = $sanitize ? _text_sanitize($instance, $field_langcode, $items[0], 'value') : $items[0]['value'];
 162            }
 163            break;
 164  
 165          // Comment related URLs.
 166          case 'url':
 167            $url_options['fragment']  = 'comment-' . $comment->cid;
 168            $replacements[$original] = url('comment/' . $comment->cid, $url_options);
 169            break;
 170  
 171          case 'edit-url':
 172            $url_options['fragment'] = NULL;
 173            $replacements[$original] = url('comment/' . $comment->cid . '/edit', $url_options);
 174            break;
 175  
 176          // Default values for the chained tokens handled below.
 177          case 'author':
 178            $replacements[$original] = $sanitize ? filter_xss($comment->name) : $comment->name;
 179            break;
 180  
 181          case 'parent':
 182            if (!empty($comment->pid)) {
 183              $parent = comment_load($comment->pid);
 184              $replacements[$original] = $sanitize ? filter_xss($parent->subject) : $parent->subject;
 185            }
 186            break;
 187  
 188          case 'created':
 189            $replacements[$original] = format_date($comment->created, 'medium', '', NULL, $language_code);
 190            break;
 191  
 192          case 'changed':
 193            $replacements[$original] = format_date($comment->changed, 'medium', '', NULL, $language_code);
 194            break;
 195  
 196          case 'node':
 197            $node = node_load($comment->nid);
 198            $title = $node->title;
 199            $replacements[$original] = $sanitize ? filter_xss($title) : $title;
 200            break;
 201        }
 202      }
 203  
 204      // Chained token relationships.
 205      if ($node_tokens = token_find_with_prefix($tokens, 'node')) {
 206        $node = node_load($comment->nid);
 207        $replacements += token_generate('node', $node_tokens, array('node' => $node), $options);
 208      }
 209  
 210      if ($date_tokens = token_find_with_prefix($tokens, 'created')) {
 211        $replacements += token_generate('date', $date_tokens, array('date' => $comment->created), $options);
 212      }
 213  
 214      if ($date_tokens = token_find_with_prefix($tokens, 'changed')) {
 215        $replacements += token_generate('date', $date_tokens, array('date' => $comment->changed), $options);
 216      }
 217  
 218      if (($parent_tokens = token_find_with_prefix($tokens, 'parent')) && $parent = comment_load($comment->pid)) {
 219        $replacements += token_generate('comment', $parent_tokens, array('comment' => $parent), $options);
 220      }
 221  
 222      if (($author_tokens = token_find_with_prefix($tokens, 'author')) && $account = user_load($comment->uid)) {
 223        $replacements += token_generate('user', $author_tokens, array('user' => $account), $options);
 224      }
 225    }
 226    elseif ($type == 'node' & !empty($data['node'])) {
 227      $node = $data['node'];
 228  
 229      foreach ($tokens as $name => $original) {
 230        switch($name) {
 231          case 'comment-count':
 232            $replacements[$original] = $node->comment_count;
 233            break;
 234  
 235          case 'comment-count-new':
 236            $replacements[$original] = comment_num_new($node->nid);
 237            break;
 238        }
 239      }
 240    }
 241  
 242    return $replacements;
 243  }

title

Description

title

Description

title

Description

title

title

Body