1

I have a client who prefers some links to have the » symbol at the end. This is typically produced with the » HTML character entity.

In Drupal 7, you would do something like this:

l('Go there »', 'node/1', array('html' => TRUE));

But how do you do it in Drupal 8? The following won't work because of new sanitization rules:

$node->toLink('Go there »', 'canonical', ['html' => TRUE]);

They want the raquo in the link, not as a suffix.

1

Translate the string

$node->toLink(t('Go there »'))

This puts the string in a translatable markup object. If you don't want to translate then use a non translatable markup \Drupal\Core\Render\Markup::create().

Both mark the string as a safe string, which you should do only for string constants like in this case, not for user input.

  • That works great, thanks. Quick followup: If I wanted to wrap part of the link text in a span, would it still be correct usage of translatable markup to do the following? $label = new TranslatableMarkup('<span>Next</span> &raquo;'); – confusedGFCI Mar 9 '18 at 18:21
  • Yes, this is OK, you find similar examples in core. – 4k4 Mar 9 '18 at 18:27
1

The cleanst solution for this is doing it in CSS:

.my_link_css_class::after {
  content: '\00a0\00bb';
}

(\00a0 is for a space, \00bb is for the »)

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.