4

In my field's description, I want to link to more documentation. If I do it like this:

$form['field_name'] = array (
  '#description' => t('Here are some words about') . ' <a href="http://mysite.com/blah">' . t('stuff') . '</a> ' . t('and things and whatnot'),
);

It works. It looks like this:

Here are some words about stuff and things and whatnot.

If I do it the proper way, like this:

$form['field_name'] = array (
  '#description' => t('Here are some words about @stuff and things and whatnot', array('@stuff' => l('stuff', 'blah')),
);

Then the HTML is generated and then output into my description as raw text, like so:

Here are some words about <a href="http://mysite.com/blah">stuff</a> and things and whatnot.

Why is the "correct" way not working?

6

Firstly, here are some of the ways NOT to pass links to the t() function:

// DO NOT DO THESE THINGS
$BAD_EXTERNAL_LINK = t('Look at Drupal documentation at !handbook.', array('!handbook' => '<a href="http://drupal.org/handbooks">'. t('the Drupal Handbooks') .'</a>'));

$ANOTHER_BAD_EXTERNAL_LINK = t('Look at Drupal documentation at <a href="http://drupal.org/handbooks">the Drupal Handbooks</a>.');

$BAD_INTERNAL_LINK = t('To get an overview of your administration options, go to !administer in the main menu.', array('!administer' => l(t('the Administer screen'), 'admin'));

And these are the accepted ways to pass that type of arg:

// Do this instead.
$external_link = t('Look at Drupal documentation at <a href="@drupal-handbook">the Drupal Handbooks</a>.', array('@drupal-handbook' => 'http://drupal.org/handbooks'));

$internal_link = t('To get an overview of your administration options, go to <a href="@administer-page">the Administer screen</a> in the main menu.', array('@administer-page' => url('admin')));

This all comes from Dynamic or static links and HTML in translatable strings, which contains justifications for that reasoning.

3
  • The first one is what I did already.
    – beth
    Jun 11 '13 at 16:04
  • @beth Do you mean the one called $BAD_EXTERNAL_LINK? That (and the other two in that block) are what not to do. The second code block contains the correct code to use. Neither of those are the same as what you tried
    – Clive
    Jun 11 '13 at 16:06
  • 1
    Whoops, yes you're right.
    – beth
    Jun 11 '13 at 16:09

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.