You are here

function messaging_template_text_replace in Messaging 6.4

Same name and namespace in other branches
  1. 6.3 messaging_template/messaging_template.module \messaging_template_text_replace()

Do token replacement.

Uses token_logic if enabled, standard token replacement otherwise

Parameters

$text: String or arry of text parts, that may be strings or template objects

$objects: Array of objects for token replacement

$language: Language object to be used for the token values

$options: Aditional options to get the tokens: language, user

1 call to messaging_template_text_replace()
messaging_template_build in messaging_template/messaging_template.module
Build message from template with token replacement

File

messaging_template/messaging_template.module, line 335
Drupal Messaging Framework - Messaging template

Code

function messaging_template_text_replace($text, $objects, $language = NULL, $options = array()) {

  // Add default options, these will work with this patch http://drupal.org/node/854688
  $options += array(
    'language' => $language ? $language : $GLOBALS['language'],
  );
  $options += array(
    'langcode' => $options['language']->language,
  );
  messaging_include('text.inc');

  // Add some token types
  $objects['global'] = NULL;

  // Parts may be text or text objects, check them and take out filters to be applied later
  if (is_object($text)) {
    $format = !empty($text->format) ? $text->format : NULL;
    $text = $text->message;
  }
  elseif (is_array($text)) {
    $filters = array();
    foreach ($text as $key => $part) {
      if (is_object($part)) {
        $text[$key] = $part->message;
        if (!empty($part->format)) {
          $filters[$key] = $part->format;
        }
      }
    }
  }

  // Use token_logic if available, http://code.developmentseed.org/token_logic
  // Otherwise use standard contrib token module, http://drupal.org/project/token
  $function = 'token_replace_multiple';
  $param_arr = array(
    $text,
    $objects,
    '[',
    ']',
    $options,
  );
  if (module_exists('token_logic')) {
    $function = 'token_logic_replace_multiple';
    $param_array = array(
      $text,
      $objects,
    );
  }
  if (is_array($text)) {
    foreach ($text as $part => $line) {
      $param_arr[0] = $line;
      $text[$part] = call_user_func_array($function, $param_arr);
    }
  }
  else {
    $text = call_user_func_array($function, $param_arr);
  }

  // Run filters if needed and return. We use our extra fast messaging_text_check_markup()
  if (is_array($text) && !empty($filters)) {
    foreach ($filters as $key => $format) {
      $text[$key] = messaging_text_check_markup($text[$key], $format);
    }
  }
  elseif (isset($format)) {

    // This means it was a single object and we have a filter to run
    $text = messaging_text_check_markup($text, $format);
  }
  return $text;
}