I have a matrix field that has a Supertable matrix within it.

I need to programatically get the ID of the field that is nested in the Matrix.

I have the code below:

$matrixField = Craft::$app->getFields()->getFieldByHandle('myMatrixFieldHandle');

Which gets me my Matrix field element, but I can't figure out how to get the fields that are within that Matrix field and thus the ID of the field within the Matrix field.

Any help appreciated!


You'll need to loop through the fields in the blocktype definition for your Matrix field. For simplicities sake, I've supplied a loop, which you can cut-down if you have a specific block type in mind you just want to fetch for:

$matrixField = Craft::$app->getFields()->getFieldByHandle('myMatrixFieldHandle');

foreach ($matrixField->getBlockTypes() as $key => $blockType) {

    foreach ($blockType->getFields() as $key => $field) {
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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