Search code examples
drupaldrupal-7drupal-fapidrupal-forms

Drupal 7: Checkbox label with links in it


is it possible to add a simple html link in the label/title of a checkbox? I tried the following code:

<?php
$form['legal']['#type'] = 'checkbox';
$form['legal']['#required'] = TRUE;
$form['legal']['#title'] = t(
  'You must accept our @tos and @legals',
  array(
    '@tos'    => l(t('terms of service'), 'node/6'),
    '@legals' => l(t('legals'), 'node/7')
  )
);
?>

But that produces the follwing label (the html markup isn't "translated"):

"You must accept our < a href="/node/6">terms of service< /a> and < a href="/node/7">legals< /a> *"

(I've added spaces after the opening brackets so that it will not be converted to the link i want to have)

Is it not possible to do such things? I Am new to drupal. Perhaps somebody can help me... Thanks!


Solution

  • This happens before you force the text to be printed as plain text.

    <?php
    $form['legal']['#type'] = 'checkbox';
    $form['legal']['#required'] = TRUE;
    $form['legal']['#title'] = t(
      'You must accept our !tos and !legals',
      array(
        '!tos'    => l(t('terms of service'), 'node/6'),
        '!legals' => l(t('legals'), 'node/7')
      )
    );
    ?>
    

    Note that you are using t() function, which acts differently on replacement's prefix. If you put @tos, it will be run through check_plain() so HTML will never be processed by the browser as it encodes HTML entities.

    !tos allows HTML markup as it will not be check_plain()'d.