I want to check the fields.field_image.content to see if it is empty or not. I want the view to display differently if there is no image in the page field_image field.

I was thinking I could just get the URL of the field_image and do a simple if statement, but I don't see a way to do that.

Is there to check if the fields.field_image.content field is empty?

I've tried a few different things like:

{% if fields.field_image.content is empty %}
{% if fields.field_image.content0|striptags|trim is empty %}

I'd like to dump fields.field_image to see the whole object, but my local setup always crashes.

Is this even possible?

The answer is yes it is possible to check if the image is present or not.

Here is how to do it:

{% set fieldimage = fields.field_image.content|render|striptags('<img>')|trim|length > 0 %}
{% if fieldimage == false %}
  image is empty
{% else %}
  image is not empty
{% endif %}

There is a raw variable available - you can use it to get raw result from the query, with all data it fetched. You can inspect it with the devel or twig_vardumper module using xdebug or dump(raw). More about debugging twig templates: https://www.drupal.org/docs/8/theming/twig/debugging-twig-templates#s-viewing-variables

  • i installed twig_vardumper module. I did a dump(raw) in my view template and it returns "null". Is that raw variable accessible in a view template? – Robbiegod Nov 28 '19 at 2:18
  • thanks for your help, the twig_vardumper module is a godsend. Previously, drupal would always crash when i tried to debug. I have solved this problem. I think it is a little whacky, but it will work. – Robbiegod Nov 28 '19 at 2:37
  • The raw variable is available in views-view-fields.html.twig. Here is a list of views template files with list of available varialbes: api.drupal.org/api/drupal/core!modules!views!views.theme.inc/… – quex Nov 28 '19 at 10:06

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.