Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kieranstartup/4500c2c82815fd341bf8dc28ab48f419 to your computer and use it in GitHub Desktop.
Save kieranstartup/4500c2c82815fd341bf8dc28ab48f419 to your computer and use it in GitHub Desktop.
Gist showing how to access matrix fields in craft cms element api
<?php
'api/faq.json' => [
'elementType' => 'Entry',
'criteria' => ['section' => 'faq'],
'transformer' => function(EntryModel $entry) {
// Array for storing your Matrix Fields
$arrayToStoreMatrixFields = [];
foreach ($entry->matrixFieldHandle as $block) {
switch ($block->type->handle) {
case 'singleMatrixFieldBlockType':
$arrayToStoreMatrixFields[] = [
'singleMatrixFieldPlainText' => $block->singleMatrixFieldPlainText,
'singleMatrixFieldRichText' => $block->singleMatrixFieldRichText->getParsedContent(),
];
break;
}
}
return [
'arrayToStoreMatrixFields' => $arrayToStoreMatrixFields
];
},
]
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment