You are here

token_filter.module in Token Filter 6

Allows token values to be used as filters.

File

token_filter.module
View source
<?php

/**
 * @file
 * Allows token values to be used as filters.
 */

/**
 * Implementation of hook_filter.
 * 
 * Adds the Token filter to the input format options.
 */
function token_filter_filter($op, $delta = 0, $format = -1, $text = '') {
  switch ($op) {
    case 'list':
      return array(
        0 => t('Token filter'),
      );
    case 'description':
      return t('Allows you to use token values in a filter enclosed. Eg. [token global site-name] or [token user user-name]');
    case 'settings':
      break;
    case 'no cache':
      return TRUE;
    case 'prepare':
      return $text;
    case 'process':

      // generate some header code, form tag and we save the node id so that the
      // javascript has a convenient place to get it from
      if (strpos($text, "[token") === FALSE) {
        return $text;
      }
      else {
        $output = preg_replace_callback("|\\[token ([^ ]*) ([^]]*)\\]|i", 'token_filter_replacetoken', $text);
        return $output;
      }
      break;
    default:
      return $text;
  }
}

/**
 * Helper function for preg_replace_callback to generate the html for each token filter
 */
function token_filter_replacetoken($matches) {
  global $user;
  $type = $matches[1];
  $token = $matches[2];
  switch ($type) {
    case 'user':
      $object = $user;
      break;
    case 'global':
    default:
      $object = NULL;
      break;
  }

  // add [ ] to the token so str_replace correctly replaces token values
  // if not, a token like 'custom_text' won't be properly replaced if another token like 'custom' exists
  $token = '[' . $token . ']';
  $output = token_replace($token, $type, $object, '[', ']');
  return $output;
}

Functions

Namesort descending Description
token_filter_filter Implementation of hook_filter.
token_filter_replacetoken Helper function for preg_replace_callback to generate the html for each token filter