Rendering of texts in MessageTemplate:getText() potentially breaks tokens

Created on 7 March 2022, over 3 years ago
Updated 28 July 2023, about 2 years ago

Problem/Motivation

In \Drupal\message\Entity\MessageTemplate::getText() the texts are rendered using \Drupal\Core\Render\Renderer::renderPlain(). We have some templates that have tokens in the HREF attribute of A elements. We have noticed that when the HREF attributes starts with a token that token breaks during rendering. All characters of the token before the last colon are stripped. Of course the broken token then is not replaced anymore during token replacement in \Drupal\message\Entity\Message::processTokens().

Example: <a href=\"[message:field_comment:entity:url]\">read comment</a>

When the token is preceded by a static URL part this problem does not occur. In that case the token is kept intact and is later properly replaced.

Example: <a href=\"/user/[message:author:uid]/edit\">edit author account</a>

Steps to reproduce

  • Install Drupal
  • Install Message module
  • Make sure to have a text format that uses the filter_html filter
  • Create e message template using that text format and include an A element with a token as HREF attribute
  • Generate a message using that message template
  • See that the token is broken and not (properly) replaced

Proposed resolution

We probably need to replace tokens before rendering the text.

Remaining tasks

  • Find the best place in the flow to replace tokens
  • Create MR
  • Review MR
  • Merge MR

User interface changes

N/A

API changes

N/A

Data model changes

N/A

πŸ› Bug report
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡³πŸ‡±Netherlands ricovandevin

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡―πŸ‡΅Japan tyler36 Osaka

    I just came across with today with Drupal 10.1 and following this guide: Drupal 10: Creating A Notification System Using The Message And ECA Modules

    Every time I enter the following message text:

    <p>
        <a href="[message:field_message_comment_ref:entity:entity:url:path]">[message:field_message_comment_ref:entity:author:name] commented on [message:field_message_comment_ref:entity:entity:title]</a>
    </p>
    

    The href gets broken and it becomes:

    <p>
        <a href="path]">[message:field_message_comment_ref:entity:author:name] commented on [message:field_message_comment_ref:entity:entity:title]</a>
    </p>
    
  • πŸ‡©πŸ‡ͺGermany a.dmitriiev

    a.dmitriiev β†’ made their first commit to this issue’s fork.

  • Pipeline finished with Failed
    13 days ago
    Total: 210s
    #552913
  • πŸ‡©πŸ‡ͺGermany a.dmitriiev

    I suggest to slightly modify the order of message text processing. The getText method in MessageTemplate entity class should not be rendered yet, but instead the getText method in Message entity class should do this, and the token processing should be done before the rendering. This way there will be also additional filter processing of the values that were replaced by tokens, for example if message template uses basic text format, but the token might have some not-allowed html tag or something, this way if rendering happens after the token replace, this makes the solution more secure (I might be wrong of course).

    To not break all other contrib modules (like message_ui), that rely on certain structure to be returned by getText method of MessageTemplate entity class, I have added one more optional parameter to it, that indicates whether it is a preview of text or real processing withing Message entity class.

    I hope this change will not break the tests.

  • Pipeline finished with Failed
    13 days ago
    Total: 378s
    #552917
  • πŸ‡©πŸ‡ͺGermany a.dmitriiev

    Of course the tests are failing :( now the built render array of a message has a Markup object and not the array anymore.

Production build 0.71.5 2024