Last active
April 10, 2017 17:10
-
-
Save kieranstartup/4500c2c82815fd341bf8dc28ab48f419 to your computer and use it in GitHub Desktop.
Gist showing how to access matrix fields in craft cms element api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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