You are here

public function MessageType::getText in Message 7

Retrieves the configured message text in a certain language.

Parameters

$langcode: The language code of the Message text field, the text should be extracted from.

$options: Array of options to pass to the metadata-wrapper:

  • 'field name': The name of the Message text field, text should be extracted from.
  • 'sanitize': Indicate if text should be escaped.

Return value

A string with the text from the field, with all the tokens converted into their actual value.

File

includes/message.message_type.inc, line 134
A class used for message types.

Class

MessageType
@file A class used for message types.

Code

public function getText($langcode = LANGUAGE_NONE, $options = array()) {

  // Set default values.
  $options += array(
    // As the text is already sanitized, it does not really matter if we
    // enable sanitizing, as it would be default. However, one can change the
    // field instance (e.g. to have no text processing) we make sure we still
    // properly sanitize the value.
    'sanitize' => TRUE,
    // The field name from which the text should be extracted.
    'field name' => MESSAGE_FIELD_MESSAGE_TEXT,
    // Determine if the text of just a single delta should be returned.
    'partials' => FALSE,
    'partial delta' => 0,
  );
  $field_name = $options['field name'];
  $params = array(
    '%field' => $field_name,
  );
  if (!($field = field_info_field($field_name))) {
    throw new MessageException(format_string('Field %field does not exist.', $params));
  }
  if (empty($field['settings']['message_text'])) {
    throw new MessageException(format_string('Field %field is not a message-text.', $params));
  }
  $property = entity_metadata_wrapper('message_type', $this);
  if (empty($langcode) && module_exists('locale')) {

    // Get the langcode from the current language.
    global $language;
    $langcode = $language->language;
  }

  // Let the metadata wrapper deal with the language.
  $property = $property
    ->language($langcode)->{$options['field name']};
  $delta = $options['partial delta'];
  $count = $field['cardinality'] == 1 ? 1 : $property
    ->count();
  if (!empty($options['partials']) && $delta >= $count) {

    // Delta is bigger than the existing field, so return early, to
    // prevent an error.
    return;
  }
  if (!empty($options['partials'])) {

    // Get partial, not the whole text.
    $property_item = $this
      ->getValue($property, $delta, $options);
    return $property_item
      ->value($options);
  }
  elseif ($property instanceof EntityListWrapper) {

    // Multiple value field.
    $output = '';
    foreach (array_keys($property
      ->value($options)) as $delta) {
      $property_item = $this
        ->getValue($property, $delta, $options);
      $output .= $property_item
        ->value($options);
    }
    return $output;
  }
  else {

    // Single value field.
    $property_item = $this
      ->getValue($property, $delta, $options);
    return $property_item
      ->value($options);
  }
}