Score:0

Check if a field is empty inside a twig paragraph template

pe flag

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 }}
pe flag
Why the downvote ? I could improve the question if I know why.
ru flag
[How do I get the raw field value in a twig template?](https://drupal.stackexchange.com/questions/228388/how-do-i-get-the-raw-field-value-in-a-twig-template) - Replace `node` with `paragraph`
pe flag
@Hudri No, I just want to check if a paragraph field is empty or not. I don't want to get the raw value.
leymannx avatar
ne flag
`{% if paragraph.field_lorem.value %}`, not `node`. Not `content`. `content` contains only fields that have been added to the display of your entity.
pe flag
@leymannx Thank you. It works perfectly. Can you post your answer ? I will accept it.
4uk4 avatar
cn flag
I didn't downvote, but you could improve the question, because the example you are quoting doesn't match with the question title. You are relying on that the field value evaluates in true or false, which is not the same as the field being empty or not. For this you can use [isEmpty()](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21TypedData%21Plugin%21DataType%21ItemList.php/function/ItemList%3A%3AisEmpty)
pe flag
@4k4 I understand. I thought `{% if node.field_lorem.value %}` was similar to `{% if node.field_lorem.value is not empty %}`. I changed the question, is it better?
4uk4 avatar
cn flag
You don't need to get the field value first. You can check the field object itself and then this works for any field type, also for those not using the property name `value`. I've added a code example as answer.
pe flag
@4k4 Thanks very much. It is much clearer for me and I will be able to improve my code with your answer (I did not know `ItemList::isEmpty`).
Score:3
cn flag

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 %}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.