You are here

function template_preprocess_message in Message 8

Same name and namespace in other branches
  1. 7 message.module \template_preprocess_message()

Prepares variables for message templates.

Default template: message.html.twig.

Parameters

array $variables: An associative array containing:

  • elements: An array of elements to display in view mode.
  • message: The message object.
  • view_mode: View mode; e.g., 'full', 'teaser', etc.

File

./message.module, line 186
API functions to manipulate messages.

Code

function template_preprocess_message(array &$variables) {
  $variables['view_mode'] = $variables['elements']['#view_mode'];

  // Provide a distinct $teaser boolean.
  $variables['teaser'] = $variables['view_mode'] == 'teaser';
  $variables['message'] = $variables['elements']['#message'];

  /** @var \Drupal\message\MessageInterface $message */
  $message = $variables['message'];
  $variables['date'] = \Drupal::service('renderer')
    ->render($variables['elements']['created']);
  unset($variables['elements']['created']);
  $variables['author_name'] = \Drupal::service('renderer')
    ->render($variables['elements']['uid']);
  unset($variables['elements']['uid']);

  // The 'page' variable is set to TRUE in two occasions:
  // - The view mode is 'full' and we are on the 'message.view' route.
  // - The message is in preview and view mode is either 'full' or 'default'.
  $variables['page'] = $variables['view_mode'] == 'full' || $variables['view_mode'] == 'default';

  // Helpful $content variable for templates.
  $variables += [
    'content' => [],
  ];
  foreach (Element::children($variables['elements']) as $key) {
    $variables['content'][$key] = $variables['elements'][$key];
  }
}