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.

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

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 %}
  • This is exactly what I needed. Thank you so much! – Sarah C Feb 14 at 21:20
  • @SarahC I think you accidentally down-voted the answer :p – No Sssweat Feb 15 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 Feb 17 at 19:46

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.