On assigning tokens(both custom and exisiting tokens) with values like

$replacements[$original] = "<b>Hello</b>";

and then using that token either in full html or any other text format(all the text formats) it displays on the screen <b>Hello</b> i.e actual tags are being rendered instead of Hello . Same is happening with other html tags i.e entire <div> <p> all the html tags get display. This although does not happen in Drupal 7.

Is there any configuration i am missing? How to render html values properly using tokens?

3 Answers 3


Try this one -

$replacements[$original] = \Drupal\Core\Render\Markup::create("<b>Hello</b>");

Helpful links:




If you just need to access the token under the html tag then another way is that, you just create token as

  switch ($name) {
   case 'custom_token':
     $text = 'Hello World!';
     $replacements[$original] = $text;

After, into the body part you can access this token with html tags.

(i.e) <b>[custom:custom_token]</b>

even i used it as screen shot enter image description here


After you replace, you have to use: 'htmlspecialchars_decode' like this:

$body = htmlspecialchars_decode($token->replace($body, $data));
  • 1
    The problem with this solution is it will decode the entire $body which may be undesirable. Ideally the solution would only affect the tokenized values.
    – Felix Eve
    Oct 16, 2017 at 2:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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