You are here

public function Message::getText in Message 7

Replace arguments with their placeholders.

Parameters

$langcode: Optional; The language to get the text in. If not set the current language will be used.

$options: Optional; Array to be passed to MessageType::getText().

Return value

mixed|string The message text compiled into a string or an array of the message partials.

1 call to Message::getText()
Message::buildContent in includes/message.message.inc
Generate an array for rendering the entity's content.

File

includes/message.message.inc, line 154
A class used for messages.

Class

Message
@file A class used for messages.

Code

public function getText($langcode = LANGUAGE_NONE, $options = array()) {
  $message_type = $this
    ->getType();
  if (!$message_type) {

    // Message type does not exist any more.
    return '';
  }
  $arguments = message_get_property_values($this, 'arguments');

  // Check if the current message have any arguments plugin.
  $handler = message_get_computed_arguments_handler($this);
  if ($handler && ($handler_arguments = $handler
    ->getArguments())) {
    $arguments = array_merge($arguments, $handler_arguments);
  }
  $output = $message_type
    ->getText($langcode, $options);
  if (!empty($arguments)) {
    $args = array();
    foreach ($arguments as $key => $value) {
      if (is_array($value) && !empty($value['callback']) && function_exists($value['callback'])) {

        // A replacement via callback function.
        $value += array(
          'pass message' => FALSE,
        );
        if ($value['pass message']) {

          // Pass the message object as-well.
          $value['callback arguments'][] = $this;
        }
        $value = call_user_func_array($value['callback'], $value['callback arguments']);
      }
      switch ($key[0]) {
        case '@':

          // Escaped only.
          $args[$key] = check_plain($value);
          break;
        case '%':
        default:

          // Escaped and placeholder.
          $args[$key] = drupal_placeholder($value);
          break;
        case '!':

          // Pass-through.
          $args[$key] = $value;
      }
    }
    $output = strtr($output, $args);
  }
  $token_replace = message_get_property_values($this, 'data', 'token replace', TRUE);
  if ($output && $token_replace) {

    // Message isn't explicetly denying token replace, so process the text.
    $context = array(
      'message' => $this,
    );
    $token_options = message_get_property_values($this, 'data', 'token options');
    $output = token_replace($output, $context, $token_options);
  }
  return $output;
}