

<p>You’ll need to start by figuring out the Matrix field’s ID. If you already have a MatrixBlockModel object as you say, you can get that via <code>$matrixBlock-&gt;fieldId</code>.</p>

<p>Once you know the Matrix field’s ID, you can get all its block types like this:</p>

<pre class="lang-php prettyprint-override"><code>$blockTypes = craft()-&gt;matrix-&gt;getBlockTypesByFieldId($matrixFieldId);
</code></pre>

<p>That will give you an array of MatrixBlockTypeModel objects. From there you can loop through each of those and get their fields like this:</p>

<pre class="lang-php prettyprint-override"><code>foreach ($blockTypes as $blockType)
{
    $fields = $blockType-&gt;getFields();

    // ...
}
</code></pre>

CC BY-SA 3.0
2014-07-07T23:49:08.587Z
Brilliant, that totally did the trick! Thanks a ton Brandon, I appreciate the quick response. :)
Lindsey D
CC BY-SA 3.0
2014-07-07T23:52:08.413Z
What is the relationship between the MatrixBlockModel and the MatrixBlockTypeModel?  I'm missing something about how the two relate to one another...  Maybe another part of this question is what is the relationship of both of these to the FieldModel?
Ben Parizek
CC BY-SA 3.0
2014-07-07T23:57:30.473Z
From $matrixBlockModel, you can do $matrixBlockModel->getType(), to get a MatrixBlockTypeModel.
Brad Bell
CC BY-SA 3.0
2014-07-08T00:01:04.733Z
@BenParizek MatrixBlockModel represents an individual Matrix block, created from within a Matrix field on an entry, etc., whereas MatrixBlockTypeModel represents a block **type**, as defined in the Matrix field’s settings (its name, its handle, and which sub-fields it has).
Brandon Kelly
CC BY-SA 3.0
2014-07-07T23:30:27.957Z
2014-07-07T23:30:27.957Z
Brandon Kelly
<p>With a MatrixBlockModel you can get the MatrixBlockTypeModel like so:</p>

<pre><code>$matrixBlockTypeModel = $matrixBlockModel-&gt;getType();
</code></pre>

<p>You can get the FieldModel of all the fields associated with that type with the <code>getFields()</code> method on the MatrixBlockTypeModel:</p>

<pre><code>$fields = $matrixBlockTypeModel-&gt;getFields();
</code></pre>

<p>If you needed the FieldLayoutFieldModel you could access them like so</p>

<pre><code>$fields = $matrixBlockTypeModel-&gt;getFieldLayout()-&gt;getFields();
</code></pre>

<p><em>NOTE: This answer misses the question as the original question requested all fields from the parent Matrix field, which would require looping through all of the MatrixBlockTypeModels as Brandon describes above.  I've tried to update this answer to at least provide some related information instead of just being wrong or a duplicate of the one above!</em></p>

CC BY-SA 3.0
2014-07-07T23:49:36.277Z
Didn't quite do it for me, but I appreciate the quick response. Thanks Ben!
Lindsey D
CC BY-SA 3.0
2014-07-07T23:50:58.740Z
Yeah, I was off base referencing the MatrixBlockModel instead of the MatrixBlockTypeModel.  I'll update this with how to connect the dots with what I learn when I have a chance!
Ben Parizek
CC BY-SA 3.0
2014-07-07T23:52:43.400Z
Fair enough, though the `MatrixBlockModel` was my starting point. Brandon's answer totally worked... but if you've got another technique, I'm definitely curious. :)
Lindsey D
CC BY-SA 3.0
2014-07-07T23:57:07.460Z
Really, I'm just trying to make my answer not crappy and wrong entirely =)
Ben Parizek
CC BY-SA 3.0
2014-07-07T23:57:46.247Z
Lol, sounds good to me!
Lindsey D
CC BY-SA 3.0
2014-07-08T05:28:05.447Z
Ben I can jump in and not even pass the breath test...
cmcjane
CC BY-SA 3.0
2014-07-08T05:30:41.850Z
*though I do have to say after listening to your training videos again this week, I was able to infer the difference between the  the MatrixBlockModel and the MatrixBlockTypeModel on the first pass, from your head to mine.  Ain't life grand?
cmcjane
CC BY-SA 3.0
2014-07-07T23:29:12.960Z
2014-07-08T00:20:57.050Z
2014-07-08T00:20:57.050Z
Ben Parizek
<p>If I want to get a list of all fields that I've defined on the site, I can do this:</p>

<pre><code>craft()-&gt;fields-&gt;getAllFields()
</code></pre>

<p>But how do I get a list of <strong>all sub-fields defined within a Matrix field?</strong></p>

<p>I have access to the <code>MatrixBlockModel</code>, if that helps.</p>

<plugin-development><matrix>
How can I list all sub-field handles of a Matrix parent field?
# How can I list all sub-field handles of a Matrix parent field?

(Asked by: username_0 on 2014-07-07)

If I want to get a list of all fields that I've defined on the site, I can do this:
craft()->fields->getAllFields()

But how do I get a list of all sub-fields defined within a Matrix field?
I have access to the MatrixBlockModel, if that helps.

**Tags:** plugin-development, matrix

---

## 2 answers

### Answer by username_1 on 2014-07-07. Score: 5 | Accepted answer

You’ll need to start by figuring out the Matrix field’s ID. If you already have a MatrixBlockModel object as you say, you can get that via $matrixBlock->fieldId.
Once you know the Matrix field’s ID, you can get all its block types like this:
$blockTypes = craft()->matrix->getBlockTypesByFieldId($matrixFieldId);

That will give you an array of MatrixBlockTypeModel objects. From there you can loop through each of those and get their fields like this:
foreach ($blockTypes as $blockType)
{
    $fields = $blockType->getFields();

    // ...
}

> **Comments:**
> **username_0**: Brilliant, that totally did the trick! Thanks a ton Brandon, I appreciate the quick response. :)  
> **username_2**: What is the relationship between the MatrixBlockModel and the MatrixBlockTypeModel?  I'm missing something about how the two relate to one another...  Maybe another part of this question is what is the relationship of both of these to the FieldModel?  
> **username_3**: From $matrixBlockModel, you can do $matrixBlockModel->getType(), to get a MatrixBlockTypeModel.  
> **username_1**: @BenParizek MatrixBlockModel represents an individual Matrix block, created from within a Matrix field on an entry, etc., whereas MatrixBlockTypeModel represents a block **type**, as defined in the Matrix field’s settings (its name, its handle, and which sub-fields it has).  

---

### Answer by username_2 on 2014-07-07. Score: 2

With a MatrixBlockModel you can get the MatrixBlockTypeModel like so:
$matrixBlockTypeModel = $matrixBlockModel->getType();

You can get the FieldModel of all the fields associated with that type with the getFields() method on the MatrixBlockTypeModel:
$fields = $matrixBlockTypeModel->getFields();

If you needed the FieldLayoutFieldModel you could access them like so
$fields = $matrixBlockTypeModel->getFieldLayout()->getFields();

NOTE: This answer misses the question as the original question requested all fields from the parent Matrix field, which would require looping through all of the MatrixBlockTypeModels as Brandon describes above.  I've tried to update this answer to at least provide some related information instead of just being wrong or a duplicate of the one above!

> **Comments:**
> **username_0**: Didn't quite do it for me, but I appreciate the quick response. Thanks Ben!  
> **username_2**: Yeah, I was off base referencing the MatrixBlockModel instead of the MatrixBlockTypeModel.  I'll update this with how to connect the dots with what I learn when I have a chance!  
> **username_0**: Fair enough, though the `MatrixBlockModel` was my starting point. Brandon's answer totally worked... but if you've got another technique, I'm definitely curious. :)  
> **username_2**: Really, I'm just trying to make my answer not crappy and wrong entirely =)  
> **username_0**: Lol, sounds good to me!  
> **username_4**: Ben I can jump in and not even pass the breath test...  
> **username_4**: *though I do have to say after listening to your training videos again this week, I was able to infer the difference between the  the MatrixBlockModel and the MatrixBlockTypeModel on the first pass, from your head to mine.  Ain't life grand?  

---

