Reputation: 55032
Does anyone know how to use the l() or url() function to create mailto links?
I am running drupal 6.
Upvotes: 13
Views: 14119
Reputation: 4251
Drupal core has this code in Drupal\Core\Field\Plugin\Field\FieldFormatter\MailToFormatter
:
$elements[$delta] = [
'#type' => 'link',
'#title' => $item->value,
'#url' => Url::fromUri('mailto:' . $item->value),
];
So you can create a render array like so:
[
'#type' => 'link',
'#title' => $email,
'#url' => Url::fromUri('mailto:' . $email),
];
Or like this:
Link::fromTextAndUrl($email, Url::fromUri('mailto:' . $email))->toRenderable();
Upvotes: 2
Reputation: 15
In Drupal 9 I found no other solution than:
$this->t('<a href="@link">This is a mail link</a>', ['@link' => 'mailto:[email protected]']);
Upvotes: 0
Reputation: 1327
Preferably none:
l() is useful for the output of internal links:
it handles aliased paths and adds an 'active' class attribute to links that point to the current page (for theming)" see reference
You need none of the above. Same goes for url(). You CAN use them, but why not keeping it simple and just use the HTML anchor tag directly.
Upvotes: 0
Reputation: 260
A good practice is to use the t() function with strings. Code should be then:
l(t('Mail me'), 'mailto:[email protected]', array('absolute' => TRUE));
Upvotes: 2
Reputation: 33275
You need to use the absolute option:
l('Mail me', 'mailto:[email protected]', array('absolute' => TRUE));
will generate
<a href="mailto:[email protected]">Mail Me</a>
Upvotes: 37