
Checking if the Drupal link field in an Entity links to an external resource
In a recent project, I needed to output an external link icon markup for links pointing to external links added via the link field of a paragraph bundle so essentially all links with http:// and https:// would have the required markup. Rather than using my own logic What I did was to just use .external at the end of the link field .url key in if condition in the relevant paragraph twig file and that was all needed to achieve it.
<span class="ext" aria-label="(link is external)"></span>
this markup was added conditionally via the following code.
{% if paragraph.field_name_of_field.0.url.external %}
<span class="ext" aria-label="(link is external)"></span>
{% endif %}
Add new comment