2

I am using Drupal 8. I have a paragraphs field (field type: paragraphs of reference revisions) in node template, I tried lots of ways to check if the field is empty (purpose is to show the field only it has a value).

<code>
{# Below codes in node.html.twig #}
{# Below codes NOT work #}
  {% if content.field_notes.0 is empty %}
    This field is empty-1.
  {% endif %}

  {% if content.field_notes[0] is empty %}
    This field is empty-2.
  {% endif %}

  {# Enable module:twig field value #}
  {% if content.field_notes|field_value is empty %}
    This field is empty-3.
  {% endif %}

  {% if node.field_notes.isempty %}
    This field is empty-4.
  {% endif %}

  {% if node.field_notes.value is empty %}
    This field is empty-5.
  {% endif %}
</code>

How can check if a paragraphs field is empty in node template in Drupal 8?

UPDATE: Codes tested and result below, the way NOT work for paragraphs but work for some other field types.

{% set count_field_element_1 = node.field_paragraphs|length %} {{ count_field_element_1 }} {# when field has or no value: count_field_element_1 = 2 #} {# paragraphs type contains two field type: image and text plain long #}

{% set count_field_element_2 = node.field_image|length %} {{ count_field_element_2 }} {# when field has value: count_field_element_2 = 1; #} {# when field has no value: count_field_element_2 = 0; #}

{% set count_field_element_3 = node.field_text_plain_long|length %} {{ count_field_element_3 }} {# when field has value: count_field_element_3 = 1; #} {# when field has no value: count_field_element_3 = 0; #}

Maybe my question is not very clear, let me explain more as below: The purpose of this question is to check if paragraphs field (not other field types) has a value. When a field value is input and saved, node page will show the field and its value; when no field value is input and saved, node page will NOT show the field (including the div element).

{% set count_field_element = node.field_paragraphs|length %} {{ count_field_element }}

count_field_element is 1 for below: enter image description here

count_field_element is 0 for below(removing all form items of paragraphs in node form): enter image description here

In short, up till now, I still NOT figure out a way to check if a paragraphs field has a value, node page will show paragraphs field empty only when removing all form items of paragraphs in node form.

2

Check on node.field_notes something like


 {% set count_paragraph_element = node.field_notes | length %}
  {% if count_paragraph_element == 0 %}
      Empty
  {% else %}
      Not empty
  {% endif %}

Update
if you want to check if text plain long field has empty value try this.
I assume that your field machine name is field_text:


{% for item in node.field_notes %}
      {% if item.entity.field_text.value is empty %}
        text value is empty
      {% else %}
        text value not empty
      {% endif %}
{% endfor %}

  • Hi berramou, please see my update above. Thanks a lot! – Basic Dec 25 '18 at 15:33
  • What about just node.field_name is empty? – Kevin Dec 25 '18 at 15:49
  • 1
    Hi Kevin, {% if node.field_paragraphs is empty %} not work for paragraphs. – Basic Dec 25 '18 at 16:13
  • node.field_name gives you a liste of item of the entity types, in this case something like EntityReferenceRevisionsFieldItemList {#1396 ▼ #list: [] #langcode: "en" #definition: FieldConfig {#967 ▶} #name: "field_paragraph" #parent: EntityAdapter {#942 ▶} #stringTranslation: null #typedDataManager: TypedDataManager {#613 ▶} } so the node.field_name always not empty. – berramou Dec 25 '18 at 16:14
  • i have tested it works for me with field of type paragraph. i get count_paragraph_element equal 0 when the field is empty, and number of items if its not. – berramou Dec 25 '18 at 16:16

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.