2
  • I have a Custom Block type.
  • It has a link field that allows <nolink>
  • In the twig template for that Custom Block type I want to check if the link field has <nolink>.

I can see there is a protected value under #url for routeName that is NULL when there is a link, and <nolink> when <nolink> is used, but I can't figure out how to check it.

I can also see there's a protected uri under #url with a similar situation. If <nolink> is used, that value is NULL. If there is a link, the link is in there.

1
  • Have you already installed the Devel sub-module Kint and then put {{ kint() }} in your template to inspect the available methods?
    – leymannx
    Commented Feb 12, 2020 at 22:42

1 Answer 1

4

Assuming your link field only allows 1 value.

{% if content.field_link.0['#url'].toString() is empty %} # or use "is not empty" if you're checking if there is a url. 
    # your code goes here
{% endif %}

If your field allows multiple link values then loop:

{% for link in content.field_link %}
  {% if link['#url'].toString() is empty %} # or use "is not empty" if you're checking if there is a url. 
    # your code goes here
  {% endif %}
{% endfor %}
3
  • This is exactly what I needed. Thank you so much!
    – Sarah C
    Commented Feb 14, 2020 at 21:20
  • @SarahC I think you accidentally down-voted the answer :p
    – No Sssweat
    Commented Feb 15, 2020 at 1:07
  • 1
    I did! I had to wait a certain amount of time to fix my vote! I felt terrible. I have fixed it. :)
    – Sarah C
    Commented Feb 17, 2020 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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