? patches
? token_profile.inc
Index: token.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/token/token.module,v
retrieving revision 1.7.4.42
diff -u -p -r1.7.4.42 token.module
--- token.module	31 Oct 2010 20:14:06 -0000	1.7.4.42
+++ token.module	1 Nov 2010 03:39:39 -0000
@@ -200,7 +200,9 @@ function token_include() {
  *   matches. Defaults to TOKEN_SUFFIX.
  * @param $options
  *   (optional) A keyed array of settings and flags to control the token
- *   generation and replacement process.
+ *   generation and replacement process. Supported options are:
+ *   - clear: A boolean flag indicating that tokens should be removed from the
+ *     final text if no replacement value can be generated.
  * @param $flush
  *   (optional) A flag indicating whether or not to flush the token cache.
  *   Useful for processes that need to slog through huge numbers of tokens
@@ -211,9 +213,7 @@ function token_include() {
  *   Text with tokens replaced.
  */
 function token_replace($text, $type = 'global', $object = NULL, $leading = TOKEN_PREFIX, $trailing = TOKEN_SUFFIX, $options = array(), $flush = FALSE) {
-  $full = token_get_values($type, $object, $flush, $options);
-  $tokens = token_prepare_tokens($full->tokens, $leading, $trailing);
-  return str_replace($tokens, $full->values, $text);
+  return token_replace_multiple($text, array($type => $object), $leading, $trailing, $options, $flush);
 }
 
 /**
@@ -238,7 +238,9 @@ function token_replace($text, $type = 'g
  *   matches. Defaults to TOKEN_SUFFIX.
  * @param $options
  *   (optional) A keyed array of settings and flags to control the token
- *   generation and replacement process.
+ *   generation and replacement process. Supported options are:
+ *   - clear: A boolean flag indicating that tokens should be removed from the
+ *     final text if no replacement value can be generated.
  * @param $flush
  *   (optional) A flag indicating whether or not to flush the token cache.
  *   Useful for processes that need to slog through huge numbers of tokens
@@ -263,6 +265,16 @@ function token_replace_multiple($text, $
     $full->values = array_merge($full->values, $temp->values);
   }
 
+  // Support clearing out tokens that would not be replaced.
+  if (!empty($options['clear'])) {
+    foreach (token_scan($text) as $token) {
+      if (!in_array($token, $full->tokens)) {
+        $full->tokens[] = $token;
+        $full->values[] = '';
+      }
+    }
+  }
+
   $tokens = token_prepare_tokens($full->tokens, $leading, $trailing);
   return str_replace($tokens, $full->values, $text);
 }
Index: token.test
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/token/token.test,v
retrieving revision 1.1.2.15
diff -u -p -r1.1.2.15 token.test
--- token.test	31 Oct 2010 20:09:14 -0000	1.1.2.15
+++ token.test	1 Nov 2010 03:39:39 -0000
@@ -78,6 +78,27 @@ class TokenUnitTestCase extends TokenTes
       $this->assertEqual($invalid_tokens, $test['invalid tokens'], 'Invalid tokens detected properly.');
     }
   }
+
+  /**
+   * Test the $options['clear'] parameter for token_replace().
+   */
+  public function testClearOption() {
+    $tests[] = array(
+      'input' => 'Foo [site-name][invalid-token] bar [invalid-token]',
+      'output' => 'Foo Drupal bar ',
+      'options' => array('clear' => TRUE),
+    );
+    $tests[] = array(
+      'input' => 'Foo [site-name][invalid-token] bar [another-invalid-token]',
+      'output' => 'Foo Drupal[invalid-token] bar [another-invalid-token]',
+      'options' => array(),
+    );
+
+    foreach ($tests as $test) {
+      $output = token_replace($test['input'], 'global', NULL, TOKEN_PREFIX, TOKEN_SUFFIX, $test['options']);
+      $this->assertIdentical($output, $test['output']);
+    }
+  }
 }
 
 class TokenNodeTestCase extends TokenTestHelper {

