From bd29797d0a70f34a4a8477f1baaf28da34715c3f Mon Sep 17 00:00:00 2001
From: Alastair Turner <bell@ctrlf5.co.za>
Date: Sat, 26 Feb 2011 16:31:41 +0200
Subject: [PATCH 1/3] Issue #1073886 by bellHead : Basic token input filter.

---
 token.module |   22 ++++++++++++++++++++++
 1 files changed, 22 insertions(+), 0 deletions(-)

diff --git a/token.module b/token.module
index 560c534..ff8d93d 100644
--- a/token.module
+++ b/token.module
@@ -938,6 +938,28 @@ function token_clean_token_name($name) {
 }
 
 /**
+ * Implementation of hook_filter_info.
+ * 
+ * Adds the Token filter to the input format options.
+ */
+function token_filter_info() {
+  $filters['filter_tokens'] = array(
+    'title' => t('Token'),
+    'description' => t('Replaces tokens in managed content.'),
+    'process callback' => '_token_filter_tokens',
+    'cache' => FALSE,
+  );
+  return $filters;
+}
+
+/**
+ *  Process callback for the token input filter
+ */
+function _token_filter_tokens($text, $filter, $format, $langcode, $cache, $cache_id) {
+  return token_replace($text);
+}
+
+/**
  * Do not use this function yet. Its API has not been finalized.
  */
 function token_render_array(array $array, array $options = array()) {
-- 
1.7.1


From 2dbf62f080991f4553429f8b231aad5f01641531 Mon Sep 17 00:00:00 2001
From: Dave Reid <dave@davereid.net>
Date: Fri, 22 Apr 2011 16:43:36 -0500
Subject: [PATCH 2/3] Issue #1073886: Added context filtering for fields.

---
 token.module |   93 ++++++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 71 insertions(+), 22 deletions(-)

diff --git a/token.module b/token.module
index ff8d93d..a557c1c 100644
--- a/token.module
+++ b/token.module
@@ -623,6 +623,77 @@ function token_form_user_admin_settings_alter(&$form, &$form_state) {
 }
 
 /**
+ * Implementation of hook_filter_info.
+ *
+ * Adds the Token filter to the input format options.
+ */
+function token_filter_info() {
+  $filters['filter_tokens'] = array(
+    'title' => t('Replace tokens'),
+    'description' => t('The usage of this filter should be restricted to trusted users only as tokens with sensitive data could be exposed.'),
+    'process callback' => '_token_filter_tokens',
+    'tips callback' => '_token_filter_tips',
+    'cache' => FALSE,
+  );
+  return $filters;
+}
+
+function token_entity_is_page($entity_type, $entity) {
+  $uri = entity_uri($entity_type, $entity);
+  return !empty($uri) && $uri['path'] == current_path();
+}
+
+/**
+ * Filter process callback for the token input filter.
+ */
+function _token_filter_tokens($text, $filter, $format, $langcode, $cache, $cache_id) {
+  $data = array();
+  $options = array();
+
+  // Attempt to fetch the entity that is being viewed via a backtrace to the
+  // field_attach_view() function if found.
+  /*$backtrace = debug_backtrace();
+  foreach ($backtrace as $caller) {
+    if ($caller['function'] == 'field_attach_view') {
+      $entity_type = $caller['args'][0];
+      $entity = $caller['args'][1];
+      if (token_entity_is_page($entity_type, $entity)) {
+        $token_type = token_get_entity_mapping('entity', $entity_type);
+        $data[$token_type] = $entity;
+        // Use the proper language code that field_attach_view was called with.
+        if ($langcode = $caller['args'][3]) {
+          $language_list = language_list();
+          if (!empty($language_list[$langcode])) {
+            $options['language'] = $language_list[$langcode];
+          }
+        }
+      }
+      break;
+    }
+    elseif ($caller['function'] == '_drupal_bootstrap_full' || $caller == 'menu_execute_active_handler') {
+      // There is no point in going back this far, so just stop.
+      break;
+    }
+  }*/
+
+  return token_replace($text, $data, $options);
+}
+
+/**
+ * Filter tip callback for the token input filter.
+ */
+function _token_filter_tips($filter, $format, $long = FALSE) {
+  if ($long) {
+    $output = t('Global tokens will be replaced with their respective token values (e.g. [site:name] or [current-page:title]). The following is a list of the tokens that are available:');
+    $output .= theme('token_tree', array('click_insert' => FALSE));
+    return $output;
+  }
+  else {
+    return t('Global tokens will be replaced with their respective token values (e.g. [site:name] or [current-page:title]).');
+  }
+}
+
+/**
  * Build a tree array of tokens used for themeing or information.
  *
  * @param $token_type
@@ -938,28 +1009,6 @@ function token_clean_token_name($name) {
 }
 
 /**
- * Implementation of hook_filter_info.
- * 
- * Adds the Token filter to the input format options.
- */
-function token_filter_info() {
-  $filters['filter_tokens'] = array(
-    'title' => t('Token'),
-    'description' => t('Replaces tokens in managed content.'),
-    'process callback' => '_token_filter_tokens',
-    'cache' => FALSE,
-  );
-  return $filters;
-}
-
-/**
- *  Process callback for the token input filter
- */
-function _token_filter_tokens($text, $filter, $format, $langcode, $cache, $cache_id) {
-  return token_replace($text);
-}
-
-/**
  * Do not use this function yet. Its API has not been finalized.
  */
 function token_render_array(array $array, array $options = array()) {
-- 
1.7.1


From fd6408720f87339461d736ff1ce47c4dafa130b5 Mon Sep 17 00:00:00 2001
From: Dave Reid <dave@davereid.net>
Date: Fri, 22 Apr 2011 16:43:51 -0500
Subject: [PATCH 3/3] Issue #1073886: Added initial test.

---
 token.test |   33 +++++++++++++++++++++++++++++++++
 1 files changed, 33 insertions(+), 0 deletions(-)

diff --git a/token.test b/token.test
index 87ffeed..dac4c7b 100644
--- a/token.test
+++ b/token.test
@@ -804,3 +804,36 @@ class TokenArrayTestCase extends TokenTestHelper {
     $this->assertTokens('array', $array, $tokens);
   }
 }
+
+class TokenFilterTestCase extends TokenTestHelper {
+  public static function getInfo() {
+    return array(
+      'name' => 'Token filter tests',
+      'description' => 'Test the token filter functionality.',
+      'group' => 'Token',
+    );
+  }
+
+  function setUp($modules = array()) {
+    parent::setUp($modules);
+
+    $filtered_html_format = filter_format_load('filtered_html');
+
+    // Add the token filter to the Full HTML input format.
+    $full_html_format = filter_format_load('full_html');
+    $full_html_format->filters['filter_tokens'] = array(
+      'status' => 1,
+    );
+    filter_format_save($full_html_format);
+
+    $this->admin_user = $this->drupalCreateUser(array(
+      'administer filters',
+      filter_permission_name($filtered_html_format),
+      filter_permission_name($full_html_format),
+    ));
+
+    $this->web_user = $this->drupalCreateUser(array('create page content', 'edit own page content'));
+    $this->drupalLogin($this->admin_user);
+  }
+
+}
-- 
1.7.1

