1

I know how to check if a field is empty in a twig template (example node--lorem.html.twig):

{% if node.field_lorem.value is not empty %}

    <p>not empty</p>

{% else %}  

    <p>empty</p>

{% endif %}  

I would like to check if a field is not empty inside a paragraph twig template (example paragraph--lorem.html.twig). But this doesn't work :

{% if content.field_lorem.value is not empty %}

or

{% if node.field_lorem.value is not empty %}

I can display this field value like this :

{{ content.field_lorem }}
9
  • Why the downvote ? I could improve the question if I know why. Commented Feb 25, 2022 at 12:07
  • 1
    How do I get the raw field value in a twig template? - Replace node with paragraph
    – Hudri
    Commented Feb 25, 2022 at 12:15
  • @Hudri No, I just want to check if a paragraph field is empty or not. I don't want to get the raw value. Commented Feb 25, 2022 at 12:41
  • 1
    {% if paragraph.field_lorem.value %}, not node. Not content. content contains only fields that have been added to the display of your entity.
    – leymannx
    Commented Feb 25, 2022 at 12:49
  • @leymannx Thank you. It works perfectly. Can you post your answer ? I will accept it. Commented Feb 25, 2022 at 12:52

1 Answer 1

4

To check if a field is empty use ItemList::isEmpty

Node

{% if node.field_example.isempty %}
  <p>Field is empty</p>
{% endif %}

Paragraph

{% if paragraph.field_example.isempty %}
  <p>Field is empty</p>
{% endif %}

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.