Answers.Body: <p>In Craft CMS 5 matrix elements are now entries, which changes the query side a bit, compared to the previous usage of MatrixBlock element, given matrix where a separate element but overall has many benefits under the entrification plan.</p>
  <p>A way to think about matrix content under Craft CMS 5:</p>
  <p><strong>Matrix Blocks become Entries</strong>  —  each block you save, e.g. a CTA field with a URL field, and a text link field, will be saved as entries in the Craft data­base. These will have some sim­i­lar prop­er­ties to the Entries you’re used to.</p>
  <p><strong>Matrix Block Types become Entry Types</strong> — you will cre­ate or choose an Entry Type for each block type you want in your Matrix field. Entry Types are a stand­alone thing now, so you can use any entry type in your Matrix block.</p>
  <p><strong>Matrix Fields become Glob­al Fields</strong>  — pri­or to Craft 5, you cre­ate fields in a Matrix Field block as one-off, non-reusable, non-glob­al fields. Now all Matrix Fields are cho­sen from stan­dard fields (via the Entry Type). So, this means you can cre­ate one, say, Rich Text Field, and reuse that fields across Entry Types and field lay­outs for all sec­tions and Matrix block types.</p>
  <p>This also means any entry related events need to factor in that matrix content may also be picked up in these now and suitable conditional logic/defensive logic is needed to ensure you don't trigger things on matrix saves (unless you want to).</p>
  <p>Reference: <a href="https://craftquest.io/articles/craft-5" rel="nofollow noreferrer">https://craftquest.io/articles/craft-5</a></p>
  <p>You may find more information useful on the changes around matrix content: <a href="https://craftquest.io/courses/craft-cms-5-guide/187755" rel="nofollow noreferrer">https://craftquest.io/courses/craft-cms-5-guide/187755</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-12-17T09:25:25.483Z
Answers.LastActivityDate: 2024-12-17T09:25:25.483Z
Answers.UserDisplayName: James White
Body: <p>I have a module that mainly does normalization of data before sending it to the frontend through Element API. We're running Craft in headless mode.</p>
  <p>Previously, I have used this code to query a Matrix field in a module:</p>
  <pre class="lang-php prettyprint-override"><code>use craft\elements\MatrixBlock;
  if ($field instanceof MatrixBlock) {
      // do stuff.
  }
  </code></pre>
  <p>But that no longer works in v5 because of how Matrix fields work now. And because Craft CMS docs are awful (i.e. they lack proper examples in all relevant languages) I have no idea how to access the data now.</p>
  <p>I tried this, but it didn't work either which really confused me.</p>
  <pre class="lang-php prettyprint-override"><code>use craft\fields\Matrix;
  if ($field instanceof Matrix) {
      // do stuff.
  }
  </code></pre>
  
Tags: <matrix><php><craft5>
Title: How do I query a Matrix field in v5, in a module (php)?
ThreadText: # How do I query a Matrix field in v5, in a module (php)?
  
  (Asked by: username_0 on 2024-12-13)
  
  I have a module that mainly does normalization of data before sending it to the frontend through Element API. We're running Craft in headless mode.
  
  Previously, I have used this code to query a Matrix field in a module:
  
  ```php
  use craft\elements\MatrixBlock;
  if ($field instanceof MatrixBlock) {
      // do stuff.
  }
  
  ```
  
  But that no longer works in v5 because of how Matrix fields work now. And because Craft CMS docs are awful (i.e. they lack proper examples in all relevant languages) I have no idea how to access the data now.
  
  I tried this, but it didn't work either which really confused me.
  
  ```php
  use craft\fields\Matrix;
  if ($field instanceof Matrix) {
      // do stuff.
  }
  
  ```
  
  **Tags:** matrix, php, craft5
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-12-17. Score: 0
  
  In Craft CMS 5 matrix elements are now entries, which changes the query side a bit, compared to the previous usage of MatrixBlock element, given matrix where a separate element but overall has many benefits under the entrification plan.
  
  A way to think about matrix content under Craft CMS 5:
  
  **Matrix Blocks become Entries**  —  each block you save, e.g. a CTA field with a URL field, and a text link field, will be saved as entries in the Craft data­base. These will have some sim­i­lar prop­er­ties to the Entries you’re used to.
  
  **Matrix Block Types become Entry Types** — you will cre­ate or choose an Entry Type for each block type you want in your Matrix field. Entry Types are a stand­alone thing now, so you can use any entry type in your Matrix block.
  
  **Matrix Fields become Glob­al Fields**  — pri­or to Craft 5, you cre­ate fields in a Matrix Field block as one-off, non-reusable, non-glob­al fields. Now all Matrix Fields are cho­sen from stan­dard fields (via the Entry Type). So, this means you can cre­ate one, say, Rich Text Field, and reuse that fields across Entry Types and field lay­outs for all sec­tions and Matrix block types.
  
  This also means any entry related events need to factor in that matrix content may also be picked up in these now and suitable conditional logic/defensive logic is needed to ensure you don't trigger things on matrix saves (unless you want to).
  
  Reference: <https://craftquest.io/articles/craft-5>
  
  You may find more information useful on the changes around matrix content: <https://craftquest.io/courses/craft-cms-5-guide/187755>
  
  
  ---
  
  