<p>You can fetch the number of entries for the first type, check if you receive an empty array if so fetch matrix blocks of type B</p>
<h2>Craft 2</h2>
<pre><code>{% set blocksTypeOne = entry.matrixFieldHandle.type('blockTypeHandle1').find() %}
{% if blocksTypeOne|length %}
     // blocks with type A exists
{% else %}
    {% set blocksWithTwo = entry.matrixFieldHandle.type('blockTypeHandle2').find()
    // do something with them
{% endif %}
</code></pre>
<h2>Update Craft 4 has finally dropped the <code>.find()</code> method</h2>
<pre><code>{% set blocksTypeOne = clone(entry.matrixFieldHandle).type('blockTypeHandle1').all() %}
{% if blocksTypeOne|length %}
    // blocks with type A exists
{% else %}
    {% set blocksWithTwo = clone(entry.matrixFieldHandle).type('blockTypeHandle2').all()
    // do something with them
{% endif %}
</code></pre>

CC BY-SA 4.0
2023-08-02T21:54:33.720Z
This technique no longer works as of Craft 4. Anyone have a solutions that works in Craft 4?
Jason McKinney
CC BY-SA 4.0
2023-08-07T14:15:56.680Z
Craft 3 deprecated the `.find()` method, you need to use `.all()` now. The rest can be basically the same
Robin Schambach
CC BY-SA 4.0
2018-04-03T19:53:27.777Z
2023-08-07T14:18:23.487Z
2023-08-07T14:18:23.487Z
Robin Schambach
<p>I have a matrix field with two different block types and I want create a conditional to check if one of the blocks is not empty show it and if it is show the other.  This is not working:</p>

<pre><code>{% for block in entry.page.type('text, intro') %}
    {% if block.type.text != "" %}
        &lt;h3&gt;{{ block.text }}&lt;/h3&gt;
    {% else %}
        {{ block.intro }}
    {% endif %}
{% endfor %}
</code></pre>

<p>Thanks!</p>

CC BY-SA 3.0
2018-04-03T19:44:46.610Z
Wouldn't it be cleaner to show all the blocks in the field? How can you make sure there is a block with the other type just because there is no block with the first type? If your field contains matrix elements from both types, what should happen then?
Robin Schambach
CC BY-SA 3.0
2018-04-03T19:46:51.323Z
This is for my search results so I just want to show one or the other. If it contained both it would just show text.
fodney
<matrix><conditional>
How to check if matrix block type is empty or not
# How to check if matrix block type is empty or not

(Asked by: username_0 on 2018-04-03)

I have a matrix field with two different block types and I want create a conditional to check if one of the blocks is not empty show it and if it is show the other.  This is not working:
{% for block in entry.page.type('text, intro') %}
    {% if block.type.text != "" %}
        {{ block.text }}
    {% else %}
        {{ block.intro }}
    {% endif %}
{% endfor %}

Thanks!

**Tags:** matrix, conditional

> **Comments:**
> **username_1**: Wouldn't it be cleaner to show all the blocks in the field? How can you make sure there is a block with the other type just because there is no block with the first type? If your field contains matrix elements from both types, what should happen then?  
> **username_0**: This is for my search results so I just want to show one or the other. If it contained both it would just show text.  

---

## 1 answers

### Answer by username_1 on 2018-04-03. Score: 5 | Accepted answer

You can fetch the number of entries for the first type, check if you receive an empty array if so fetch matrix blocks of type B
Craft 2
{% set blocksTypeOne = entry.matrixFieldHandle.type('blockTypeHandle1').find() %}
{% if blocksTypeOne|length %}
     // blocks with type A exists
{% else %}
    {% set blocksWithTwo = entry.matrixFieldHandle.type('blockTypeHandle2').find()
    // do something with them
{% endif %}

Update Craft 4 has finally dropped the .find() method
{% set blocksTypeOne = clone(entry.matrixFieldHandle).type('blockTypeHandle1').all() %}
{% if blocksTypeOne|length %}
    // blocks with type A exists
{% else %}
    {% set blocksWithTwo = clone(entry.matrixFieldHandle).type('blockTypeHandle2').all()
    // do something with them
{% endif %}

> **Comments:**
> **username_2**: This technique no longer works as of Craft 4. Anyone have a solutions that works in Craft 4?  
> **username_1**: Craft 3 deprecated the `.find()` method, you need to use `.all()` now. The rest can be basically the same  

---

