23

What is the best method to check if a field is empty in a node template.

In node--example.html.twig we can use "content" variable.

I can't use check like checking a block

{% if content.field_example %} 

I can use something like this:

{% if content.field_example['#object'] is defined %} 

But what is the best method?

1
  • 1
    Use hook_preprocess and check programmatically, maybe add indicator variable. Otherwise what 4k4 wrote. Commented Apr 20, 2016 at 7:53

5 Answers 5

32

Like I also wrote in How can you render fields from an entity reference in node templates? [Drupal 8], content is a render array that contains data prepared for displaying.

If you want to check or compare something, use the values instead, which are available on the node object.

{% if node.field_example.value %}.

Note that the property depends on the field type. If it's a reference field, you need to use target_id instead. The referenced question links to a entity field API cheat sheet that lists common field types and their properties.

1
  • For image field {% node.field_article_image.target_id %} Commented Dec 2, 2019 at 13:14
20

You can't be sure about the structure of the render array inside of content, so this is possible not the best method.

This may be a better solution:

{% if content.field_example|render|striptags|trim %} <p>field is not empty</p> {% endif %} 

This checks if rendering the field generates any output.

But this depends, how you have configured the field format and what you try to do. For example, you could have configured to display a label if the field is empty. Then you have to adjust this. If you don't depend on the rendered output and only want to check, if the field has a value in the database, use the node object. See Berdir's answer.

0
10
{% if not (node.field_whatever.isEmpty ?? true) and content.field_whatever is defined %} <div class="whatever_markup"> {{ content.field_whatever }} </div> {% endif %} 

This solution is quite bullet-proof and even working in reusealbe and/or shared templates (e.g. a shared node--teaser.html.twig accross multiple bundles) because

  • it works for all field types
  • it checks if the field exists
  • it checks if the field is not empty
  • it checks if the field is not hidden in display mode

This should work for any entity template, e.g. you can simply replace node.field_whatever with paragraph.field_whatever in a paragraph.html.twig template file.

2
  • Sorry, after your edit, did you miss a check? Shouldn't be if not (node.field_whatever.isEmpty ?? true) and content.field_whatever is defined? Otherwise as you wrote, doesn't work, at least for me with a filled field on D10. Commented Nov 17, 2023 at 18:07
  • Oh, of course, you're right, fixed it now Commented Nov 17, 2023 at 21:43
6

Check if field is not empty in Twig templating (Field Collection)
Template: field-collection-item--field-services.html.twig

Install Field Twig Value Module to get the value using "|field_value", followed by "is not empty"

{% if content.field_work|field_value is not empty %} ... {% endif %} 
1

Option for content fields:

{% if content.field_name['#items'].getValue() %} 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.