You are here

public function MessageTemplate::getText in Message 8

Retrieves the configured message text in a certain language.

Parameters

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

int $delta: Optional; Represents the partial number. If not provided - all partials will be returned.

Return value

array An array of the text field values. These will have been processed for their corresponding text formats.

Overrides MessageTemplateInterface::getText

File

src/Entity/MessageTemplate.php, line 261

Class

MessageTemplate
Defines the Message template entity class.

Namespace

Drupal\message\Entity

Code

public function getText($langcode = Language::LANGCODE_NOT_SPECIFIED, $delta = NULL) {
  $text = $this->text;
  $language_manager = \Drupal::languageManager();
  if ($language_manager instanceof ConfigurableLanguageManagerInterface) {
    if ($langcode == Language::LANGCODE_NOT_SPECIFIED) {

      // Get the default language code when not specified.
      $langcode = $language_manager
        ->getDefaultLanguage()
        ->getId();
    }
    if ($this->langcode !== $langcode) {
      $config_translation = $language_manager
        ->getLanguageConfigOverride($langcode, 'message.template.' . $this
        ->id());
      $translated_text = $config_translation
        ->get('text');

      // If there was no translated text, we return nothing instead of falling
      // back to the default language.
      $text = $translated_text ?: [];
    }
  }

  // Process text format.
  foreach ($text as $key => $item) {

    // Call the renderer directly instead of adding a dependency on the Filter
    // module's check_markup() function.
    // @see check_markup()
    $build = [
      '#type' => 'processed_text',
      '#text' => isset($item['value']) ? $item['value'] : '',
      '#format' => $item['format'],
      '#langcode' => $langcode,
    ];
    $text[$key] = \Drupal::service('renderer')
      ->renderPlain($build);
  }
  if (isset($delta)) {

    // Return just the delta if it exists. Always wrap in an array here to
    // ensure compatibility with methods calling getText.
    return isset($text[$delta]) ? [
      $text[$delta],
    ] : [];
  }
  return $text;
}