Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Create and Render link in drupal_set_message

By editor , 22 December, 2015
Question

How to create /  render html link in drupal_set_message() with Drupal 8 ?

Example : Password Reset Link.
      $link = \Drupal::l(t('Link'), \Drupal\Core\Url::fromRoute('user.pass'));
      drupal_set_message(t("Password Reset Link : @link!", ['@link!' => $link]));

 

In a form, You can use it like:

$form['a_link'] = [
'#type' => 'markup',
'#markup' => t('Password Reset Link : @link', array('@link' => $link)),
];

The l() function \Drupal::l() is deprecated.


Use \Drupal\Core\Link instead
Example:
use \Drupal\Core\Link;
$link = Link::fromTextAndUrl($text, $url);
#OR
$link = \Drupal\Core\Link::fromTextAndUrl($text, $url);

Add Link to drupal_set_message (Examples)

use Drupal\Core\Link;
use Drupal\Core\StringTranslation\TranslatableMarkup;

$link = Link::createFromRoute('Link title (User Login)', 'user.login')
->toString();
$text = new TranslatableMarkup("The link is : @link)", ["@link" => $link]);
drupal_set_message($text, 'ok');

Get HTML (generated) Link
$link_html = Link::createFromRoute('Link title (User Login)', 'user.login')
->toString()
->getGeneratedLink();
kint($link_html);

 

Tags

  • Drupal 8
  • Code
  • Add new comment
Create a link with Drupal 8 like l() and url() on D7

Comments2

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

lykyd (not verified)

10 years 10 months ago

What is the mt() function ?

What is the mt() function ?

  • Reply

editor

10 years 6 months ago

In reply to What is the mt() function ? by lykyd (not verified)

Sorry, that's t(); not mt();

Sorry, that's t(); not mt();

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer