You are here

token_filter.module in Token Filter 7

Additional text filter for token input.

File

token_filter.module
View source
<?php

/**
 * @file
 * Additional text filter for token input.
 */

/**
 * Implements hook_filter_info().
 */
function token_filter_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_filter_tokens',
    'tips callback' => '_token_filter_filter_tips',
    'cache' => FALSE,
  );
  return $filters;
}

/**
 * Filter process callback for the token text filter.
 */
function _token_filter_filter_tokens($text, $filter, $format, $langcode, $cache, $cache_id) {
  $data = array();
  $options = array(
    'clear' => TRUE,
  );

  // Attempt to figure out the current context based on the current backtrace.
  $backtrace = debug_backtrace();
  array_shift($backtrace);

  // Pop off this current function in the stack.
  foreach ($backtrace as $caller) {
    switch ($caller['function']) {
      case 'field_default_view':

        // field_default_view($entity_type, $entity) is fairly reliable since
        // it is called by both field_attach_view() and field_view_field().
        $entity_type = $caller['args'][0];
        $entity = $caller['args'][1];
        $token_type = token_get_entity_mapping('entity', $entity_type);
        $data[$token_type] = $entity;

        // Use the proper language code that field_default_view() was called with.
        if ($langcode = $caller['args'][4]) {
          $language_list = language_list();
          if (!empty($language_list[$langcode])) {
            $options['language'] = $language_list[$langcode];
          }
        }
        break;
      case '_drupal_bootstrap_full':
      case 'menu_execute_active_handler':
        break 2;
    }
  }
  return token_replace($text, $data, $options);
}

/**
 * Filter tip callback for the token input filter.
 */
function _token_filter_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]).');
  }
}

Functions

Namesort descending Description
token_filter_filter_info Implements hook_filter_info().
_token_filter_filter_tips Filter tip callback for the token input filter.
_token_filter_filter_tokens Filter process callback for the token text filter.