I installed the Message module on my site. I created a custom module that created a message when a comment is created.


How to display the parent node or product of a comment with TWIG?

In Drupal tokens, there is no face-up token with the parent entity.

Here is my template for comments :​

 * @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="{{ drupal_token('message:author:url', {message: message}, {clear: true}) }}" title="{{ drupal_token('message:author:display-name', {message: message}, {clear: true}) }}">
      <img src="{{ drupal_token('message:author:user_picture:thumbnail', {message: message}, {clear: true}) }}" alt="{{ drupal_token('message:author:user_picture:alt', {message: message}, {clear: true}) }}">
  <div class="timeline-content mb-3">
    <div class="card">
      <div class="card-header text-center">
        Nouveau commentaire
      <div class="card-body">
        <div class="card-text">
          <h6>par <a href="{{ drupal_token('message:author:url', {message: message}, {clear: true}) }}" title="{{ drupal_token('message:author:display-name', {message: message}, {clear: true}) }}">{{ drupal_token('message:author:display-name', {message: message}, {clear: true}) }}</a>, il y a {{ drupal_token('message:created:since', {message: message}, {clear: true}) }}</h6>
          {{ content.flag_mark_message }}
      <div class="card-img-top">
        <a href="{{ drupal_token('message:field_comment_reference:entity:url', {message: message}, {clear: true}) }}" title="{{ drupal_token('message:field_comment_reference:entity:cid', {message: message}, {clear: true}) }}">
          <img src="/themes/custom/bootstrap_subtheme_front_office/images/comment.jpg?itok=edKIlv1_" alt="image commentaire" typeof="Image" class="img-responsive">
      <div class="card-body">
        <h3 class="card-title mb-0">
          <a href='{{ drupal_token('message:field_comment_reference:entity:url', {message: message}, {clear: true}) }}'>Commentaire N°{{ drupal_token('message:field_comment_reference:entity:cid', {message: message}, {clear: true}) }}</a>
        sur le contenu <a href='{{ drupal_token('message:field_node_reference:entity:url', {message: message}, {clear: true}) }}'>{{ drupal_token('message:field_node_reference:entity:title', {message: message}, {clear: true}) }}</a>
      <div class="card-footer text-center">
        <small class="text-muted"></small>
          <a class="timeline-link" href='{{ drupal_token('message:field_comment_reference:entity:url', {message: message}, {clear: true}) }}'>
            Voir le commentaire

Your Answer

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

Browse other questions tagged or ask your own question.