Why doesn't the token for the node title work? 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.


  


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.


  


[message:node-title] is replaced from the title of the parent node, but [message:field_node_reference:entity:title] is not replaced. Why?
[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-&gt;bundle() == 'example_create_comment') {
        $replacements[$original] = $message-&gt;field_comment_reference-&gt;entity-&gt;getCommentedEntity()-&gt;label();
      }
      else {
        $replacements[$original] = $message-&gt;field_node_reference-&gt;get(0)-&gt;entity-&gt;label();
      }
      break;


When the message bundle (the message template) is example_create_comment, it gets the node title using the field_comment_reference field ($message-&gt;field_comment_reference-&gt;entity-&gt;getCommentedEntity()-&gt;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.