Hi,

I think this is one of most basic use cases but I really didnt find anything that solved my problem.

I just would like to know how to check with TWIG if a field value is not empty.

The following snippets didnt work:

{% if content.field_myfield %}
{% endif }

{% if content.field_myfield|length %}
{% endif }

{% if not empty content.field_myfield %}
{% endif }

I really dont know why this basic stuff is not documentated. Maybe I am wrong.

Thanks for your help.

Comments

jfauske’s picture

Hi,

Try to alter your statements just a little bit:

{% if content.field_myfield is not empty %}
{% endif %}

Also the twig docs at sensiolabs are usually a great resource.

Edit: Sorry, I was a bit quick to just note on the syntax. There's an explanation here (with a "workaround")...

--Jørn

mo86’s picture

Thanks for your help! This helped me a lot! As you said your code example doesn't work but the workaround did the job.

Honestly I dont understand why I have to use an annoying workaround to get the most basic functionality in a templateengine based CMS running. I mean to check if a field is empty or not... That should be the first thing to get fixed.

Sometimes I can't understand where they set the focus in drupal 8 development. I hope there is an easy way in drupal 8.1.

Jeff Burnz’s picture

It's not a "workaround", that is how it works. There are many very good reasons to late render in twig (look them up), which make it basically impossible to reliably check for emptiness without rendering.

The render filter is the easy way. You can use the function also, different syntax same result.

mo86’s picture

Okay, got your point. But I think then it would be nice if this would be explained somewhere.

You can read nothing about it here: https://www.drupal.org/theme-guide/8
and also nothing here: http://sqndr.github.io/d8-theming-guide

Aöso they don't mentioned that a render filter exists and how to use it.

So it is nice that all the core developers know how to deal with all of this but for the average theme developer its really hard to find out how the basic stuff works in D8.

Thats just my opinion.

Jeff Burnz’s picture

Documentation is everyones responsibility, if you can add to the docs because you learnt something, please do. Most core developers are pretty busy people, they don't always have time to update the docs, and they're fallible just like all of us, maybe someone just plain forgot. No problem, see it as an opportunity to contribute.

mo86’s picture

"Most core developers are pretty busy people, they don't always have time to update the docs," I think here we got the problem ;) But anyway I will try to update the documentation soon.

jfauske’s picture

Looking at this some more, there actually could be an easy way.

You need to make the entity object available in your template and use that to access an entity field's value.
For user and node entities this is already handled for you in template_preprocess_user() and template_preprocess_node().

So in a node template you could actually do this already:

{% if node.field_myfield is not empty %}
{% endif %}

(just tested it myself using a standard field_image)

from bartik's node.html.twig:

---
 * Available variables:
 * - node: The node entity with limited access to object properties and methods.
     Only "getter" methods (method names starting with "get", "has", or "is")
     and a few common methods such as "id" and "label" are available. Calling
     other methods (such as node.delete) will result in an exception.
---

--Jørn

mo86’s picture

Thanks this works also fine! Now it would be great if "the drupal community" would decide which one is the right way to to do it :) Would be nice to have one way which also works for views.

Jeff Burnz’s picture

You are the community, so decide for yourself what works for you in your situation, with your problem. Drupal always has many ways to do things, for both generic and specific reasons. That said, there are safer ways of doing things, ways that will almost always work - those are the ones I like to suggest because they cause less problems for people who don't have the skill to debug and figure it out for themselves.

Jeff Burnz’s picture

A lot of the time this will be OK, but there will be occasions when it does not work, which is why render is so important, are you 100% positive it will work all the time with any field, truth is probably not, but that doesn't mean you should not use it, indeed use it where appropriate.

jfauske’s picture

From looking at FieldItemList and FieldItemListInterface and trying to make sense of the related classes and interfaces, yes I'm pretty confident twig_test_empty() will return true for all empty entity fields.

I'm a bit curious as to which occasions you think this will not work?

Anyway I'm not going to argue in any way on the importance of render.

--Jørn

illutek’s picture

{% if content.field_poster[0] is not empty %}
   <div class="superteaser__programma__poster">
      {{ content.field_poster }}
   </div>
{% endif %}

In my case had to add [0}

hellobank’s picture

{% if (content.field_catagory|field_value) ==10 %}

<h1>Yes, the value is 10</h1>
{% endif %}

above code is NOT work. 

akozoriz’s picture

You have to install https://www.drupal.org/project/twig_field_value module. After that your code will work.

rwohleb’s picture

I'm currently using

{% if paragraph.field_image.isEmpty() %}

in a paragraph template. The isEmpty function should be available for any field on any entity.

grubshka_v2’s picture

Thanks rwohleb, this seems to work for any field, in any case !

alexpertsi’s picture

{% if content.field_bkgimg[0] is empty %}
  <div class="nobkgimg">
    {{ content.body }}
  </div>
{% endif %}
{% if content.field_bkgimg[0] is not empty %}
  <div class="withbkgimg">
    {{ content.body }}
    {{ content.field_bkgimg }}
  </div>
{% endif %}
alexpertsi’s picture

Also on Twig template, if you want to check if a field has a value equal to something, this worked for me on views page

{% if node.field_hstyle.value == "layout3" %}
  <div>layout3</div>
{% endif %}
plato1123’s picture

I had to end up having to use this to check if my array was empty:

{% if fields.field_tags.content|render|striptags|trim %}

<div class="article-homepage-featured article-homepage-featured__tags">in

<ul class="tags tags--white">

<li class="tags__item tags__link">{{ fields.field_tags.content }}</li>

</ul>

</div>

{% endif %}
afagioli’s picture

This worked fine inside views-view-fields.html.twig with:

{% if field.content|render|striptags|trim %}

Thanks for sharing

DravenDev’s picture

Really goes without saying that to customize you must enter the templates of the specific place to be able to verify if a field is empty or not. But there are different ways to achieve it, in addition the functionality also varies according to the type of project.

Verify that a field is empty with multiple purposes.

  • To print or not what comes in the value.
  • To act with the value stored in the value of the field.
  • To add behavioral js functionality in your project.
  • As important data to send in a context.
  • To add a specific behavior of the template.
  • There are n-utilities that you can give it... Etc.

However, the community publishes the most used cases here a little more according to what you can build, similar when you program in android and when you create events to an element (image, button, text, etc), you can also customize your contents in drupal.

Expensive (render) check

{% if content.field|render is not empty %}
  {{ content.field }}
{% endif %}

Note that this can be an expensive (resource-wise) check to make.

Basic check

{% if node.field.value %}
  {{ content.field }}
{% endif %}

Entity Reference fields

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

Review: Take the trouble to check out the community, to find more ways to solve a problem. Check Stackoverflow account whit similar issue

Logical filedset

{% if '1' in content['field_logical']['#items'].getValue()|first.value %}
{#procedures}
{% endif %}

Happy code!

Review this links for community

VasyOK’s picture

ok, how to check logical field?

if content.field_logical is not empty - doesn't work
if content.field_logical.value == "On" - doesn't work

WeedWho’s picture

Same here, I try and it's not working 

VasyOK’s picture

My friend say solution for me:

{% if node.field_logical.value %}

or

{% if node.field_logical.value == "1" %}
Ridha Rahmi’s picture

if node twig

{% if node.field_name is not empty %}
// your code here
{% endif %}

if paragraph twig

{% if paragraph.field_name is not empty %}
 // your code here
{% endif %}
darvanen’s picture

This explains why using render can have unexpected effects and shows how to ensure your template only renders things once:

https://www.previousnext.com.au/blog/right-way-check-empty-content-twig