Skip to main content
remove deprecated find function
Source Link
Robin Schambach
  • 19.8k
  • 1
  • 20
  • 45

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 %}

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

{% 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 %}

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 %}
Source Link
Robin Schambach
  • 19.8k
  • 1
  • 20
  • 45

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

{% 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 %}