1

In Drupal 7 we have option to mark HTML=>TRUE.

How can i do it in Drupal 8

my code is

    $create_icon_path = 'my_icon.png'; //my icon path
    $create_icon = '<img height=15 src = "/' . $create_icon_path . '">';
    $create_earlywarning_url = Url::fromUserInput('/node/5');
    $create_earlywarning = \Drupal::l($create_icon, $create_earlywarning_url);
    return $create_earlywarning;

But its printing the

"<img height=15 src = "my_create-icon.png">" in <a> tag.
2

Here is a way to do this:

$create_icon_path = 'my_icon.png'; //my icon path
$create_icon = '<img height=15 src = "/' . $create_icon_path . '">';  
$create_earlywarning_url = Url::fromUserInput('/node/5');  
$link_render_array = array(
  '#title' => array(
    '#markup' => $create_icon
  ),
  '#type' => 'link',
  '#url' => $create_earlywarning_url,
);
return \Drupal::service('renderer')->renderRoot($link_render_array);

Hope this solves it!

1

The 'html' => TRUE option from links was removed from core for a few different reasons. Read the issue for more information.

One of the keys to understanding Drupal 8 is that you almost never need to explicitly render things or resort to the \Drupal class in object oriented code.

When you are in a controller or in any other context when you are returning content to be displayed, leverage render arrays and Drupal will do the right thing:

$create_icon_path = '/some/path/my_icon.png';
$create_earlywarning_url = Url::fromUserInput('/node/5');

$build = [
  '#title' => [
    '#theme' => 'image',
    '#uri' => $create_icon_path,
    '#width' => 15,
    '#height' => 15,
    '#alt' => 'My Alt Text',
  ],
  '#type' => 'link',
  '#url' => $create_earlywarning_url,
];

return $build;

So the key here, is that for the #title of the link, you can just make it a render array for an image, and the renderer will figure it out and output everything properly (eg, also include the RDF info if you have that enabled).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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