Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 5 down vote accepted

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();

    // ...
}
share|improve this answer
    
Brilliant, that totally did the trick! Thanks a ton Brandon, I appreciate the quick response. :) –  Lindsey D Jul 7 at 23:49
    
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 Jul 7 at 23:52
    
From $matrixBlockModel, you can do $matrixBlockModel->getType(), to get a MatrixBlockTypeModel. –  Brad Bell Jul 7 at 23:57
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). –  Brandon Kelly Jul 8 at 0:01

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!

share|improve this answer
    
Didn't quite do it for me, but I appreciate the quick response. Thanks Ben! –  Lindsey D Jul 7 at 23:49
    
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 Jul 7 at 23:50
    
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 Jul 7 at 23:52
1  
Really, I'm just trying to make my answer not crappy and wrong entirely =) –  Ben Parizek Jul 7 at 23:57
    
Lol, sounds good to me! –  Lindsey D Jul 7 at 23:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.