<p>This should do it for a text field:</p>

<pre><code>craft.entries.section('aboutus').textField(':empty:')|length
</code></pre>

<p>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:</p>

<pre><code>{% set entries = craft.entries.section('news') %}

{% set validEntries = [] %}

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

<p><code>validEntries</code> is now an array of entries that have at least one block.</p>

CC BY-SA 3.0
2016-03-15T22:34:51.310Z
thanks! But now I have a matrix. How I get the real length here?
Jonathan Schiffner
CC BY-SA 3.0
2016-03-15T23:18:14.870Z
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
CC BY-SA 3.0
2016-03-15T23:23:28.267Z
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
CC BY-SA 3.0
2016-03-20T15:41:43.753Z
And is there a way to check if an `lightswitch Field` is on or not?
Jonathan Schiffner
CC BY-SA 3.0
2016-03-15T16:55:45.660Z
2016-03-16T00:27:49.750Z
2016-03-16T00:27:49.750Z
Brad Bell
<p>Some links in the menu should only be shown if the textfield is not empty. For example:</p>

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

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

<p>So, how I get the real length of this textfield? </p>

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

CC BY-SA 3.0
2016-03-15T23:27:34.117Z
"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
CC BY-SA 3.0
2016-03-15T23:33:53.720Z
And want to see if the Matrix field has any blocks
Jonathan Schiffner
<templating>
Check length of textfield in header file
# Check length of textfield in header file

(Asked by: username_0 on 2016-03-15)

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?

**Tags:** templating
> **Comments:**
> **username_1**: "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?  
> **username_0**: And want to see if the Matrix field has any blocks  

---

## 1 answers

### Answer by username_1 on 2016-03-15. Score: 2 | Accepted answer

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.

> **Comments:**
> **username_0**: thanks! But now I have a matrix. How I get the real length here?  
> **username_1**: 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?  
> **username_0**: 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  
> **username_0**: And is there a way to check if an `lightswitch Field` is on or not?  

---

