Drupal 7: Checkbox label with links in it
Asked Answered
U

2

5

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!

Unwieldy answered 1/11, 2012 at 18:40 Comment(0)
C
6

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.

Cowshed answered 1/11, 2012 at 21:53 Comment(1)
Thank you for this helpful answer! I searched for something like this all the time, but was not lucky. I should have read the documentation about the t() function more carefully.Unwieldy
M
1

Instead you can try to use #prefix.

$form['legal']['#prefix'] = t(
  'You must accept our @tos and @legals',
  array(
    '@tos'    => l(t('terms of service'), 'node/6'),
    '@legals' => l(t('legals'), 'node/7')
  )
);

Hope this works... Muhammad.

Mannerly answered 1/11, 2012 at 19:33 Comment(4)
This does not help. But I'm confused that this still does not print HTML although the Form API Reference says that...Unwieldy
-1. This is entirely wrong. FAPI allows HTML markup but @tos forces text to be check_plain().Cowshed
Big thx Ayesh. That helped me out! Without the placeholders, everything works just fine.Unwieldy
@Muhammad no offense I just needed to point that with a downvote. Hope it's fine :)Cowshed

© 2022 - 2024 — McMap. All rights reserved.