I have Drupal 8 with the Message module.

I created message templates using drupal 8 but I prefer to use TWIG.

In Drupal 8 my code works, but when I put it in TWIG, the tokens do not work.

How to use the Message Template ?

 * @file
 * Default theme implementation for message entities.
 * Available variables:
 * - $content: An array of comment items. Use render($content) to print them all, or
 *   print a subset such as render($content['field_example']). Use
 *   hide($content['field_example']) to temporarily suppress the printing of a
 *   given element.
 * - $title: The (sanitized) entity label.
 * - $url: Direct url of the current entity if specified.
 * - $page: Flag for the full page state.
 * - $classes: String of classes that can be used to style contextually through
 *   CSS. It can be manipulated through the variable $classes_array from
 *   preprocess functions. By default the following classes are available, where
 *   the parts enclosed by {} are replaced by the appropriate values:
 *   - entity-{ENTITY_TYPE}
 * Other variables:
 * - $classes_array: Array of html class attribute values. It is flattened
 *   into a string within the variable $classes.
 * @see template_preprocess()
 * @see template_preprocess_message()
 * @see template_process()
<div{{ attributes }}>

<div class="timeline">
  <div class="timeline-icon">
    <a href="[message:author:url]" title="[message:author:display-name]">
      <img src="[message:author:user_picture:thumbnail]" alt="[message:author:user_picture:alt]">
  <div class="timeline-content mb-3">
    <div class="card">
      <div class="card-header text-center">
        Nouvelle boutique
      <div class="card-body">
        <div class="card-text">
          <h6>par <a href="[message:author:url]" title="[message:author:display-name]">[message:author:display-name]</a>, il y a [message:created:since]</h6>
      <div class="card-img-top">
        <a href="[message:field_store_reference:entity:url]" title="[message:field_store_reference:entity:name]">
          <img src="[message:field_store_reference:entity:field_personnel_logo:diaporama_lg_50][message:field_store_reference:entity:field_professionnel_logo:diaporama_lg_50]" alt="[message:field_store_reference:entity:field_personnel_logo:alt][message:field_store_reference:entity:field_professionnel_logo:alt]" typeof="Image" class="img-responsive">
      <div class="card-body">
        <h3 class="card-title mb-0">
          <a href='[message:field_store_reference:entity:url]'>[message:field_store_reference:entity:name]</a>
      <div class="card-footer text-center">
        <small class="text-muted"></small>
          <a class="timeline-link" href='[message:field_store_reference:entity:url]'>
            Voir la boutique test

  {{ content }}

enter image description here


It doesn't look like you've got properly formatted twig here - you're inserting Drupal tokens right into the template. That's not going to work, you're going to need to preprocess them somehow, and use proper twig. This template isn't any different from any others, so you can look at existing working templates for help. This will get you started on some best practices with preprocessing and Twig: https://www.drupal.org/docs/8/theming/twig/twig-best-practices-preprocess-functions-and-templates

Also, have you looked at the Message documentation? https://www.drupal.org/node/2919768

You're either going to need to process those tokens or use field values, the raw tokens won't work in the template.

Hope this gets you in the right direction!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.