By jhodgdon on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
9.2.x
Introduced in version:
9.2.0
Description:
We have added 2 new Twig functions for making safe links in help topic Twig templates. See https://www.drupal.org/docs/develop/documenting-your-project/help-topic-... for more information about creating help topics.
These functions output a link if the URL generation works and the URL is accessible by the current user. If there is a failure of generation or access, plain text is returned instead. There are two functions:
- help_route_link(text, route, {'param1': 'value'}) -- make a generic link to a route
- help_topic_link(topic.id) -- make a link to a topic, using the topic title as the link text
Usage examples:
{% set non_route_link = render_var(help_route_link('not a route', 'not_a_real_route')) %}
{% set missing_params_link = render_var(help_route_link('missing params', 'valid.route.needs.int.param')) %}
{% set invalid_params_link = render_var(help_route_link('invalid params', 'valid.route.needs.int.param', {'int_param': 'not_an_int'})) %}
{% set valid_link = render_var(help_route_link('valid link', 'valid.route.needs.int.param', {'int_param': 2})) %}
{% set topic_link = render_var(help_topic_link('valid.topic.id')) %}
{% set not_a_topic = render_var(help_topic_link('not_a_topic')) %}
<p>{% trans %}Examples of the new link functions:{% endtrans %}</p>
<ul>
<li>{% trans %}Should not be a link: {{ non_route_link }}{% endtrans %}</li>
<li>{% trans %}Should not be a link: {{ missing_params_link }}{% endtrans %}</li>
<li>{% trans %}Should not be a link: {{ invalid_params_link }}{% endtrans %}</li>
<li>{% trans %}Should be a link if user has access: {{ valid_link }}{% endtrans %}</li>
<li>{% trans %}Should be a link: {{ topic_link }}{% endtrans %}</li>
<li>{% trans %}Should not be a link: {{ not_a_topic }}{% endtrans %}</li>
</ul>
Note that in a real topic, you should translate any literal text, such as link text. Example of how to do that:
{% set layout_link_text %}
{% trans %}Block layout{% endtrans %}
{% endset %}
{% set layout_link = render_var(help_route_link(layout_link_text, 'block.admin_display')) %}
<ol>
<li>{% trans %}In the <em>Manage</em> administrative menu, navigate to <em>Structure</em> > {{ layout_link }}.{% endtrans %}</li>
...
Impacts:
Module developers