Drupal 11: Wrapping A Link In A HTML Element

The link render option will render a link without any surrounding HTML elements.

If you want to wrap it in an element you can use the "child" element of the "html_tag" render element to wrap the link.

$output['p_wrapped_link'] = [
  '#type' => 'html_tag',
  '#tag' => 'p',
  'child' => [
    '#type' => 'link',
    '#title' => $this->t("Home."),
     '#url' => Url::fromRoute('<front>'),
  ],
];

This will render something like this:

<p><a href="https://www.example.com/">Home</a></p>

Add new comment

The content of this field is kept private and will not be shown publicly.