4

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?

5

Try this one -

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

Helpful links:

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21Markup.php/class/Markup/8.2.x

http://www.drupaldump.com/drupal-8-how-have-custom-tokens-html-them-properly-replaced

| improve this answer | |
0

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

$body = htmlspecialchars_decode($token->replace($body, $data));
| improve this answer | |
  • 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 '17 at 2:13
0

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;
   break;

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

| 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.