0

As you can see in the code below, I try various ways to prevent Drupal from escaping the HTML and passing the link tags to the output, including the use of '#allowed_tags' and the Markup class. None of these approaches are working.

$text = t('Administer books');

$markup = new Markup();
$link = $markup->create(Link::fromTextAndUrl($text, $url)->toString());

$items[] = $link . ': ' . t('Browse books and edit fields') . '.';

$text = t('Import books');
$url = new Url('mei_marc.import');

$items[] = Link::fromTextAndUrl($text, $url)->toString() . ': ' . t('Visit this page to import a book') . '.';

$array['mei_marc_list'] = array(
  '#theme' => 'item_list',
  '#list_type' => 'ul',
  '#title' => '',
  '#items' => $items,
  '#allowed_tags' => ['a'],
);

$txt = drupal_render($array);

$markup = new Markup();

$list = $markup->create($txt);

return ['#markup' => $list];

The output is the following.

<div class="item-list">
<ul><li>&lt;a href=&quot;/mei_marc/list&quot;&gt;Administer books&lt;/a&gt;: Browse books and edit fields.</li>
<li>&lt;a href=&quot;/admin/mei_library/import&quot;&gt;Import books&lt;/a&gt;: Visit this page to import a book.</li>
</ul>
</div>

1 Answer 1

1
$items[] = [
  '#markup' => Link::fromTextAndUrl($text, $url)->toString() . ': ' . t('Visit this page to import a book') . '.',
];

$form['mei_marc_list'] = [
  '#theme' => 'item_list',
  '#items' => $items,
];

return $form;
1
  • Wow. I didn't see that approach anywhere in my searches, and it works.
    – arnoldbird
    Mar 31, 2020 at 18:54

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.