<p>you have to call</p>

<pre><code>$block-&gt;text-&gt;getParsedContent()
</code></pre>

<p>or another method of <a href="https://craftcms.com/classreference/fieldtypes/RichTextData" rel="nofollow noreferrer">RichTextData</a> Model</p>

CC BY-SA 3.0
2017-09-05T17:20:08.743Z
2017-09-05T17:20:08.743Z
toy
<p>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"?</p>

<p><a href="https://craftcms.stackexchange.com/qcuestions/13118/how-do-you-use-the-element-api-plugin-to-output-matrix-blocks-for-an-entry">https://craftcms.stackexchange.com/qcuestions/13118/how-do-you-use-the-element-api-plugin-to-output-matrix-blocks-for-an-entry</a></p>

<pre><code>&lt;?php
namespace Craft;

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

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

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

<p><a href="https://i.sstatic.net/UNpnJ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/UNpnJ.png" alt="screenshot of matrix block"></a></p>

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

<pre><code>'content' =&gt; $block-&gt;type-&gt;bodyText
</code></pre>

<matrix><json>
Output Matrix Block with Element API Plugin
# Output Matrix Block with Element API Plugin

(Asked by: username_0 on 2016-04-29)

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
 [
        '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,
                ];
            }
        ],
    ],
];


This is the only line that is breaking my JSON. Any help would be greatly appreciated. 
'content' => $block->type->bodyText

**Tags:** matrix, json

---

## 1 answers

### Answer by username_1 on 2017-09-05. Score: 1

you have to call
$block->text->getParsedContent()

or another method of RichTextData Model


---

