1

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!

1 Answer 1

1

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) {
    Craft::dump($blockType->name);

    foreach ($blockType->getFields() as $key => $field) {
        Craft::dump($field->name);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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