You are here

function _token_filter_filter_tokens in Token Filter 7

Filter process callback for the token text filter.

1 string reference to '_token_filter_filter_tokens'
token_filter_filter_info in ./token_filter.module
Implements hook_filter_info().

File

./token_filter.module, line 25
Additional text filter for token input.

Code

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);
}