? token_example/token_example.tokens.inc
Index: token_example/token_example.info
===================================================================
RCS file: token_example/token_example.info
diff -N token_example/token_example.info
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ token_example/token_example.info	6 Mar 2010 23:28:35 -0000
@@ -0,0 +1,9 @@
+; $Id:$
+name = Token example
+description = An example module showing how to define and use tokens.
+package = Example modules
+version = VERSION
+core = 7.x
+files[] = token_example.module
+files[] = token_example.tokens.inc
+files[] = token_example.test
Index: token_example/token_example.module
===================================================================
RCS file: token_example/token_example.module
diff -N token_example/token_example.module
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ token_example/token_example.module	6 Mar 2010 23:28:35 -0000
@@ -0,0 +1,136 @@
+<?php
+// $Id$
+
+/**
+ * Implements hook_menu().
+ */
+function token_example_menu() {
+  $items['examples/token'] = array(
+    'title' => 'Token example',
+    'description' => 'Test replacement tokens in real time.',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('token_example_example_form'),
+    'access callback' => TRUE,
+  );
+  return $items;
+}
+
+/**
+ * Form builder; display lists of supported token entities and text to tokenize.
+ */
+function token_example_example_form(&$form, $form_state) {
+  // Scan through the list of entities for supported token entities.
+  $entities = entity_get_info();
+  foreach ($entities as $entity => $info) {
+    $object_callback = "_token_example_get_{$entity}";
+    if (function_exists($object_callback) && $objects = $object_callback()) {
+      $form[$entity] = array(
+        '#type' => 'select',
+        '#title' => $info['label'],
+        '#options' => array(0 => t('Not selected')) + $objects,
+        '#default_value' => isset($form_state['storage'][$entity]) ? $form_state['storage'][$entity] : 0,
+        '#access' => !empty($objects),
+      );
+    }
+  }
+
+  $form['text'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Enter your text here'),
+    '#default_value' => 'Hello [current-user:name]!'
+  );
+
+  // Display the results of tokenized text.
+  if (!empty($form_state['storage']['text'])) {
+    $data = array();
+    foreach ($entities as $entity => $info) {
+      if (!empty($form_state['storage'][$entity])) {
+        $objects = entity_load($entity, array($form_state['storage'][$entity]));
+        if ($objects) {
+          $data[$entity] = reset($objects);
+        }
+      }
+    }
+
+    $form['text_tokenized'] = array(
+      '#type' => 'item',
+      '#title' => t('Result'),
+      '#markup' => filter_xss(token_replace($form_state['storage']['text'], $data)),
+    );
+  }
+
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Submit'),
+  );
+
+  return $form;
+}
+
+/**
+ * Submit callback; store the submitted values into storage.
+ */
+function token_example_example_form_submit($form, &$form_state) {
+  $form_state['storage'] = $form_state['values'];
+  $form_state['rebuild'] = TRUE;
+}
+
+/**
+ * Build a list of available content.
+ */
+function _token_example_get_node() {
+  if (!user_access('access content') && !user_access('bypass node access')) {
+    return array();
+  }
+
+  // Build a list of available nodes.
+  $node_query = db_select('node', 'n');
+  $node_query->fields('n', array('nid', 'title'));
+  $node_query->condition('n.status', NODE_PUBLISHED);
+  $node_query->orderBy('n.created', 'DESC');
+  $node_query->range(0, 10);
+  $node_query->addTag('node_access');
+  $nodes = $node_query->execute()->fetchAllKeyed();
+  $nodes = array_map('check_plain', $nodes);
+  return $nodes;
+}
+
+/**
+ * Build a list of available comments.
+ */
+function _token_example_get_comment() {
+  if (!user_access('access comments') && !user_access('administer comments')) {
+    return array();
+  }
+
+  // Build a list of available comments.
+  $comment_query = db_select('comment', 'c');
+  $comment_query->innerJoin('node', 'n', 'n.nid = c.nid');
+  $comment_query->fields('c', array('cid', 'subject'));
+  $comment_query->condition('n.status', NODE_PUBLISHED);
+  $comment_query->condition('c.status', COMMENT_PUBLISHED);
+  $comment_query->orderBy('c.created', 'DESC');
+  $comment_query->range(0, 10);
+  $comment_query->addTag('node_access');
+  $comments = $comment_query->execute()->fetchAllKeyed();
+  $comments = array_map('check_plain', $comments);
+  return $comments;
+}
+
+/**
+ * Build a list of availalbe user accounts.
+ */
+function _token_example_get_user() {
+  if (!user_access('access user profiles') && !user_access('administer users')) {
+    return array();
+  }
+
+  $account_query = db_select('users', 'u');
+  $account_query->fields('u', array('uid', 'name'));
+  $account_query->condition('u.uid', 0, '>');
+  $account_query->condition('u.status', 1);
+  $account_query->range(0, 10);
+  $accounts = $account_query->execute()->fetchAllKeyed();
+  $accounts = array_map('check_plain', $accounts);
+  return $accounts;
+}
Index: token_example/token_example.test
===================================================================
RCS file: token_example/token_example.test
diff -N token_example/token_example.test
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ token_example/token_example.test	6 Mar 2010 23:28:35 -0000
@@ -0,0 +1,48 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Test cases for Testing the token example module.
+ */
+
+class TokenExampleTestCase extends DrupalWebTestCase {
+  protected $web_user;
+
+  public static function getInfo() {
+    return array(
+      'name' => 'Token example functionality',
+      'description' => 'Verify the token example interface.',
+      'group' => 'Examples',
+    );
+  }
+
+  function setUp() {
+    parent::setUp('token_example');
+    $this->web_user = $this->drupalCreateUser();
+    $this->drupalLogin($this->web_user);
+  }
+
+  function testInterface() {
+    $this->drupalGet('examples/token');
+    $this->assertNoFieldByName('node');
+    $this->assertNoFieldByName('user');
+
+    $edit = array(
+      'text' => 'User [current-user:name] is trying the token example!',
+    );
+    $this->drupalPost(NULL, $edit, t('Submit'));
+    $this->assertText('User ' . $this->web_user->name . ' is trying the token example!');
+
+    $node = $this->drupalCreateNode(array('title' => 'Example node', 'status' => NODE_PUBLISHED));
+    $this->drupalGet('examples/token');
+    $this->assertFieldByName('node');
+
+    $edit = array(
+      'text' => 'Would you like to view the [node:type-name] [node:title]?',
+      'node' => $node->nid,
+    );
+    $this->drupalPost(NULL, $edit, t('Submit'));
+    $this->assertText('Would you like to view the Basic page Example node?');
+  }
+}
