1

I want to make the checkbox title as link with target _blank but this doesn't seem working.

$form['approve'] = array(
  '#type' => 'checkbox',
  '#title' => l(t('Approve conditions'), 'node/10'),
  '#required' => TRUE,
);

enter image description here

So the approve conditions should be a link

6
  • 1
    So why not a simple list ul ? checkbox is for select something...
    – Kevin
    Commented Jun 28, 2017 at 11:49
  • Do you mean the users should select the checkbox if they approve the conditions, and the link is for taking them to the page listing the conditions?
    – apaderno
    Commented Jun 28, 2017 at 11:59
  • @kiamlaluno. Yes, i do mean it. Since it is not working, I have to do like this, which is not nice $form['approve'] = array( '#type' => 'checkbox', '#title' => t('<a href="/node/10" target= _blank>Approve conditions</a>'), '#required' => TRUE, ); and the title cannot be translated anymore.
    – Tony
    Commented Jun 28, 2017 at 12:04
  • Just to be sure I understand, is the problem adding the target, and make it _blank?
    – apaderno
    Commented Jun 28, 2017 at 12:09
  • I want the title to be a link and to be translated and the target = _blank. As you see if i add <a href ... in the title, it won't be translated
    – Tony
    Commented Jun 28, 2017 at 12:19

1 Answer 1

2

That code doesn't work on Drupal 8 because l() has been removed from Drupal 8. The correct code for Drupal 8 is closer to the following one.

use Drupal\Core\Link;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;

$form['approve'] = array(
  '#type' => 'checkbox',
  '#title' => Link::fromTextAndUrl(new TranslatableMarkup('Approve conditions'), Url::fromUri('entity:node/10', ['attributes' => ['target' => '_blank']]))->toString(),
  '#required' => TRUE,
);

Alternatively, you can put the link in the description.

use Drupal\Core\Link;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;

$form['approve'] = array(
  '#type' => 'checkbox',
  '#title' => new TranslatableMarkup('Approve conditions'),
  '#description' => new TranslatableMarkup('By clicking this checkbox, you <a href=":conditions">approve the conditions</a>', [':conditions' => Url::fromUri('entity:node/10', ['attributes' => ['target' => '_blank']])]),
  '#required' => TRUE,
);

Keep in mind that, as said in the comments for l() and url() are removed in favor of a routing based URL generation API, if the shown code is used in a class using the StringTranslationTrait trait, instead of the TranslatableMarkup class you use $this->t().

4
  • Thanks, It works, but when I change the 'entity : node/10' to 'entity : term-and-condition' then it is not working. BTW, how do you know to use link, Url and StringTranslation\TranslatableMarkup in this case?
    – Tony
    Commented Jun 28, 2017 at 12:52
  • Ok the entity is for {entity_type}/{entity_id}, how about the url string?
    – Tony
    Commented Jun 28, 2017 at 12:56
  • How can I replace node/10 with term-and-condition?
    – Tony
    Commented Jun 28, 2017 at 13:09
  • ok just replace the 'entity:node/10' with 'base:/term-and-condition'. Many thanks guys
    – Tony
    Commented Jun 28, 2017 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.