I have a paragraph called "Square cta" that I'm using like an old field collection. It has 2 fields in it - a text field (field_cta_text), and an Image (field_square_cta_image).

I have a content type called "Show Page" in which I will upload an image and put in the cta text for.

I then have a view that shows all of the show pages and I reference the square_cta in my template - node--view--seasons.html.twig - like so

<a href="{{ url }}">{{ content.field_square_cta }}</a>

This works great for displaying the image inside the field_square_cta for the show and it acting as a link back to the Show Page node it's in.

Where I am having trouble, is not all shows have the field_square_cta populated. For those show pages, I want to just display the of the node instead. I have been unable to figure out how to write my if logic to determine if the field_square_cta is undefined.

Here is what I was trying to do:

{%  if content.field_square_cta %}
  <a href="{{ url }}">{{ content.field_square_cta }}</a>
{%  else %}
  <h1><a class="title" href="{{ url }}">{{ label }}</a></h1>
{% endif %}

The conditional is always going with the if, never the else, even when I don't have any data inside the content.field_square_cta. I have been unable to determine the proper syntax for checking if that is empty or not.

Any help would be greatly appreciated.

  • Instead of checking for the field, check for the first value of the field {% if content.field_square_cta.0 %} – Hudri Mar 20 '18 at 15:43
  • I have tried this as well and still always getting a positive when I'm expecting it to be empty – choneyse Mar 20 '18 at 23:46
  • It sounds like maybe, with paragraphs, an "empty" paragraph gets saved with the node even if there is no content in the paragraph (drupal.org/project/paragraphs/issues/2459881) that might be why the is empty check is not wokring. Not sure how to get around this? – choneyse Mar 20 '18 at 23:59

Better use the node object to check whether the field is not empty:

{% if not node.field_example.isempty %}
  • I have tried {% if not node.field_square_cta.isempty %} and {% if not node.field_square_cta is empty %} and neither is working. They both are giving false positives - meaning when I expect the field is empty, it is behaving as if it is not - the same that was happening to me on my initial post. – choneyse Mar 20 '18 at 23:41
  • if there is an issue with the empty check of the field (you've mentioned in a comment to the question), then this would only apply to the code of this answer, because the code in the question checks only if there is a render array present, which always is, also if the field is really empty. – 4k4 Mar 21 '18 at 7:23

Turns out I had to go look at the fields inside the paragraph to determine if they were empty or not as the paragraph always returned an empty array. My final logic ended up as (note this includes my actual field names instead of the example fields names in my original question)

{% if content.field_square_cta.0['#paragraph'].field_square_cta_image.target_id %}


Better to simply check the length of the array...

{% if node.field_example.value|length > 0 %}

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.