Does anyone know how to use the l() or url() function to create mailto links?
I am running drupal 6.
Does anyone know how to use the l() or url() function to create mailto links?
I am running drupal 6.
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>
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));
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();
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.
In Drupal 9 I found no other solution than:
$this->t('<a href="@link">This is a mail link</a>', ['@link' => 'mailto:[email protected]']);
© 2022 - 2024 — McMap. All rights reserved.