2

Some links in the menu should only be shown if the textfield is not empty. For example:

{% if craft.entries.section('aboutus').textField|length == 0 %} style="display: none;" {% endif %}.

But craft.entries.section('aboutus').textField|length always returns 1. I also tried is empty.

So, how I get the real length of this textfield?

And I have the same problem with a matrix. How can I check if the Matrix field has any blocks?

  • "How can I check if the matrix is empty or not?" Does that mean you want to see if your Matrix field has any blocks? Or you want to see if a particular field inside of a Matrix block is empty? – Brad Bell Mar 15 '16 at 23:27
  • And want to see if the Matrix field has any blocks – Jonathan Schiffner Mar 15 '16 at 23:33
2

This should do it for a text field:

craft.entries.section('aboutus').textField(':empty:')|length

To see if a Matrix field has any blocks, it's not as pretty. There's probably a better way, but it's all I can think of at the moment:

{% set entries = craft.entries.section('news') %}

{% set validEntries = [] %}

{% for entry in entries %}
    {% if entry.matrixField|length %}
        {% set validEntries = valid|merge([entry]) %}
    {% endif %}
{% endfor %}

validEntries is now an array of entries that have at least one block.

| improve this answer | |
  • thanks! But now I have a matrix. How I get the real length here? – Jonathan Schiffner Mar 15 '16 at 22:34
  • So textField is in a Matrix block inside of a Matrix field? Can you update the question with the name of the block and Matrix field? – Brad Bell Mar 15 '16 at 23:18
  • No, for the textField your version works fine. But now I have the same problem with a Matrix Block. But wait. I will update the question – Jonathan Schiffner Mar 15 '16 at 23:23
  • And is there a way to check if an lightswitch Field is on or not? – Jonathan Schiffner Mar 20 '16 at 15:41

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.