In Drupal 8, I activated the Message module and the Message Example sub-module.

These are the fields I added to the template to create a message when a new comment is created.

enter image description here

This is the content of the message.

1) [message:node-title]
2) [message:field_node_reference:entity:title]

These are the tokens that are listed as available.

enter image description here

[message:node-title] is replaced from the title of the parent node, but [message:field_node_reference:entity:title] is not replaced. Why?

enter image description here


[message:field_node_reference:entity:title] is not replaced with the node title because, for a example_create_comment message template, the field_node_reference field doesn't contain any valid node reference.

In fact, the Message example module uses the following code to return the value of the [message:node-title] token. (See message_example_tokens().)

    case 'node-title':
      if ($message->bundle() == 'example_create_comment') {
        $replacements[$original] = $message->field_comment_reference->entity->getCommentedEntity()->label();
      else {
        $replacements[$original] = $message->field_node_reference->get(0)->entity->label();

When the message bundle (the message template) is example_create_comment, it gets the node title using the field_comment_reference field ($message->field_comment_reference->entity->getCommentedEntity()->label()), not the field_node_reference field that is used for the other message templates.

The [message:field_node_reference:entity:title] token is added from the Token module, which adds a token for each field a content entity has. (Message is a content entity, and Message template is its bundle.) The Token module doesn't know if there is another field to use when the field_node_reference field doesn't contain any valid reference, so the token is not replaced.

If you want to get the node title in any case, you need to use the [message:node-title] token.

| improve this answer | |

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.