2

Below is my code to output the news entires under the "news" section of my site using the Element API. My question is how do I output the matrix block with a block type of "text" with a field called "blockText"?

https://craftcms.stackexchange.com/qcuestions/13118/how-do-you-use-the-element-api-plugin-to-output-matrix-blocks-for-an-entry

<?php
namespace Craft;

return [
    'endpoints' => [
        'api/entries' => [
            'elementType' => 'Entry',
            'criteria' => ['section' => 'news'],
            'transformer' => function(EntryModel $entry) {
              // Check to see if there are any images
              $featuredImage = $entry->featuredImage->first();
              $bodyBlocks = [];
               foreach ($entry->bodyEnhanced as $block) {

                   switch ($block->type->handle) {
                       case 'text':
                           $bodyBlocks[] = [
                            //'text' => $block->type->attributeConfigs,
                            'content' => $block->type->bodyText
                           ];
                       break;
                   }
               }

                return [
                    'id' => $entry->id,
                    'url' => $entry->url,
                    'postDate' => $entry->postDate,
                    'title' => $entry->title,
                    'excerpt' => !empty($entry->excerpt) ? $entry->excerpt : false,
                    'image' =>   !empty($featuredImage) ? $featuredImage->url : false,
                    'body' => $bodyBlocks,
                ];
            }
        ],
    ],
];

screenshot of matrix block

This is the only line that is breaking my JSON. Any help would be greatly appreciated.

'content' => $block->type->bodyText
1

you have to call

$block->text->getParsedContent()

or another method of RichTextData Model

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.