Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-02T21:54:33.720Z
Answers.Text: This technique no longer works as of Craft 4. Anyone have a solutions that works in Craft 4?
Answers.UserDisplayName: Jason McKinney
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-07T14:15:56.680Z
Answers.Text: Craft 3 deprecated the `.find()` method, you need to use `.all()` now. The rest can be basically the same
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-04-03T19:53:27.777Z
Answers.LastActivityDate: 2023-08-07T14:18:23.487Z
Answers.LastEditDate: 2023-08-07T14:18:23.487Z
Answers.UserDisplayName: Robin Schambach
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-04-03T19:44:46.610Z
Comments.Text: 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?
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-04-03T19:46:51.323Z
Comments.Text: 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.
Comments.UserDisplayName: fodney
Tags: <matrix><conditional>
Title: How to check if matrix block type is empty or not
ThreadText: # 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 != "" %}
          <h3>{{ block.text }}</h3>
      {% 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  
  
  ---
  
  