I miss the deprecated l()
a lot. I am trying to construct the Drupal 8 equivalent of:
l(t('mylink'), 'admin/structure/types');
This call generates this HTML:
'<a href="/admin/structure/types">mylink</a>'
I want to know how to produce the same HTML (i.e. a string) using the Link class.
Please understand that I am not looking asking how to render the link. I need the string for other purposes than rendering. I fully aware of that usually, one wants to render the link. If that is what you want to do, you should look at the accepted answer to this question: How do I create a link?.
I am trying to generate an HTML link to'admin/structure/types'
. But will appreciate an answer that explains how to do this for any valid internal path.
I've looked at a lot of examples, a most of the them end up saying one should use Link::fromTextAndUrl
in some construct like this - without explaining how to get $routename from the path.:
Link::fromTextAndUrl(t('mylink'), Url::fromRoute($routeName));
However, by searching (as suggested by Jaypan) I was able to determine that the routename for this path was 'entity.node_type.collection'
. This works as one would expect:
$url = Url::fromRoute('entity.node_type.collection');
$url_string = $url->toString();
I.e.: $url_string
is now 'admin/structure/types'
.
So I proceed to build a link object
$link = Link::fromTextAndUrl(t('mylink'), Url::fromRoute('entity.node_type.collection'));
Now: How to get an HTML string?
No Sssweat suggests:
$url = Url::fromRoute('book.admin');
$link_thingy = Link::fromTextAndUrl(t('mylink'), $url)->toString();
This step just make $link_thingy
into an object. Close, but no cigar.
In an updated version of his answer, Jaypan says that treating this object like a string - example:
print Link::fromTextAndUrl(t('mylink'), Url::fromRoute('user.login'))->toString();
outputs the HTML string. This works. This means you will have the HTML string when you use it in a context one would use a string. Therefore, I've accepted Jaypan's answer.
I've also tried out the kiamlaluno's answer, i.e.:
$link_thingy = $link->toRenderable();
This produces a (huge) render array, not a string. A render array, however, is a very useful thing (see comments for why this is so), but it is not accepted as answer to this question because the question was: How to get an HTML string?
Environment: Clean install of Drupal 8.8.6 (core + devel). Ubuntu 20.04 LTS.
'#type' => 'link'
and don't render too early (yes, toString() is also rendering). Unless you need the link in a string for other purposes not connected with building page content. – 4k4 Jun 1 '20 at 9:35