Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By joe casey on
I'm writing a message to the user with drupal_set_message(). I'd like to include a link, but the link gets escaped and doesn't work.
How do I wrap the link inside the message so that it is not escaped, or, build some object that will be rendered as a working link?
Thanks.
Comments
Here's how
First, make sure $message is something you have built yourself and is not passing along user input, otherwise you are exposing yourself to attack.
Drupal 8 drupal_set_message with a link example
Here is an example of drupal_set_message in Drupal 8 with a link.
\Drupal::l() is deprecated
\Drupal::l() is now deprecated, here's what worked for me:
Don't pass links directly to t function
Don't paste the link inside the t function because you will have to translate the link with its markup. Do this instead:
$link = \Drupal\Core\Link::fromTextAndUrl('the link title', \Drupal\Core\Url::fromUri("internal:/node/1"))->toString();
drupal_set_message(t("Go back to @link", array('@link' => $link)));
That is not quite right
That is not quite right either as according to https://www.drupal.org/docs/7/api/localization-api/dynamic-or-static-lin...
Based upon your example it should be:
Security Risk
Per the documentation, do not use
@
or%
replacement methods within HTML attributes (such as href), JavaScript, or CSS. Doing so is a security risk.