<p>It is possible with a <code>foreach</code>, something like this:</p>
<pre><code>'transformer' =&gt; function(Entry $entry) {
   $values = [];
   foreach($entry-&gt;fieldHandle as $block){
    $values[] = $block['fieldMatrixHandle'];
   }
   return [
     'data' =&gt; $values,
   ];
 },
</code></pre>

CC BY-SA 4.0
2021-02-26T16:46:19.377Z
2021-02-26T16:46:19.377Z
Alexis Dalai Waldo Jiménez
<p>Totally possible. Just loop through the blocks and add whatever data you want from them to the response array:</p>

<pre class="lang-php prettyprint-override"><code>'transformer' =&gt; function(EntryModel $entry) {
    // Create an array of all the "Body" Matrix field's blocks
    $bodyBlocks = [];
    foreach ($entry-&gt;body as $block) {
        switch ($block-&gt;type-&gt;handle) {
            case 'text':
                $bodyBlocks[] = [
                    'text' =&gt; $block-&gt;myRichTextField-&gt;getParsedContent(),
                ];
                break;
            case 'image':
                $image = $block-&gt;myAssetsField-&gt;first();
                $bodyBlocks[] = [
                    'image' =&gt; $image ? $image-&gt;getUrl(['width' =&gt; 500]),
                    'caption' =&gt; $block-&gt;myPlainTextField,
                ];
                break;
        }
    }

    return [
        'title' =&gt; $entry-&gt;title,
        'url' =&gt; $entry-&gt;url,
        'description' =&gt; (string) $entry-&gt;description,
        'body' =&gt; $bodyBlocks
    ];
},
</code></pre>

<p>Each of the elements in that sub-array will be <a href="https://craftcms.com/classreference/models/MatrixBlockModel">MatrixBlockModel</a> objects.</p>

CC BY-SA 3.0
2016-01-06T22:59:10.347Z
I seem to be able to grab almost all of the metadata about the field but not the fields within the actual blocks. Adding `'text' => $block->text,` to my endpoint def returns `Property 'Craft\MatrixBlockModel.text' is not defined.`. My handle is `text` for a rich text field.
Paulo
CC BY-SA 3.0
2016-01-06T23:22:39.693Z
@Paulo Do you have multiple block types? Maybe one of them doesn’t have a `text` field.
Brandon Kelly
CC BY-SA 3.0
2016-01-06T23:37:57.690Z
Yeah, I have multiple blocktypes: The `textBlock` type has a `text` field inside it (which is "rich text" but that shouldn't matter). I also have a few more with various fieldtypes inside of them. E.G. `imageBlock` has an assets field inside of it but I haven't tried grabbing it yet. Trying to sort our the basics. Thanks a ton for your help with this!
Paulo
CC BY-SA 3.0
2016-01-07T11:41:13.340Z
@Paulo Just updated the example to show how you could do it with multiple block types. Note that if you have a Rich Text field, you will need to call that `getParsedConent()` method, which was added in [Craft 2.5.2753](https://craftcms.com/changelog#build2753).
Brandon Kelly
CC BY-SA 3.0
2016-01-07T17:57:15.457Z
Thanks a ton for the updates to the examples! I'll take a look and report back if I hit any issues.
Paulo
CC BY-SA 3.0
2016-01-07T22:08:35.177Z
things seem to be working! Thank you so much for all of your help this. Where in the can I learn more about things like "getParsedConent()" and some more of these core concepts that I can't seem to easily find documentation on. The Fractal docs help a little but I don't want to keep pestering you all :)
Paulo
CC BY-SA 3.0
2016-01-06T21:05:34.933Z
2016-01-07T11:39:35.293Z
2016-01-07T11:39:35.293Z
Brandon Kelly
<p>I am having a hard time trying to figure out how to pull matrix blocks within an entry into the single entry's endpoint with the Element API plugin.</p>

<p>Is this possible? Or do I need to output the blockId's and then create another API endpoint to query by the matrix block id?</p>

<plugin-elementapi>
How do you use the Element API plugin to output Matrix blocks for an entry?
# How do you use the Element API plugin to output Matrix blocks for an entry?

(Asked by: username_0 on 2016-01-06)

I am having a hard time trying to figure out how to pull matrix blocks within an entry into the single entry's endpoint with the Element API plugin.

Is this possible? Or do I need to output the blockId's and then create another API endpoint to query by the matrix block id?

**Tags:** plugin-elementapi

---

## 2 answers

### Answer by username_2 on 2016-01-06. Score: 14 | Accepted answer

Totally possible. Just loop through the blocks and add whatever data you want from them to the response array:
'transformer' => function(EntryModel $entry) {
    // Create an array of all the "Body" Matrix field's blocks
    $bodyBlocks = [];
    foreach ($entry->body as $block) {
        switch ($block->type->handle) {
            case 'text':
                $bodyBlocks[] = [
                    'text' => $block->myRichTextField->getParsedContent(),
                ];
                break;
            case 'image':
                $image = $block->myAssetsField->first();
                $bodyBlocks[] = [
                    'image' => $image ? $image->getUrl(['width' => 500]),
                    'caption' => $block->myPlainTextField,
                ];
                break;
        }
    }

    return [
        'title' => $entry->title,
        'url' => $entry->url,
        'description' => (string) $entry->description,
        'body' => $bodyBlocks
    ];
},

Each of the elements in that sub-array will be MatrixBlockModel objects.

> **Comments:**
> **username_0**: I seem to be able to grab almost all of the metadata about the field but not the fields within the actual blocks. Adding `'text' => $block->text,` to my endpoint def returns `Property 'Craft\MatrixBlockModel.text' is not defined.`. My handle is `text` for a rich text field.  
> **username_2**: @Paulo Do you have multiple block types? Maybe one of them doesn’t have a `text` field.  
> **username_0**: Yeah, I have multiple blocktypes: The `textBlock` type has a `text` field inside it (which is "rich text" but that shouldn't matter). I also have a few more with various fieldtypes inside of them. E.G. `imageBlock` has an assets field inside of it but I haven't tried grabbing it yet. Trying to sort our the basics. Thanks a ton for your help with this!  
> **username_2**: @Paulo Just updated the example to show how you could do it with multiple block types. Note that if you have a Rich Text field, you will need to call that `getParsedConent()` method, which was added in [Craft 2.5.2753](https://craftcms.com/changelog#build2753).  
> **username_0**: Thanks a ton for the updates to the examples! I'll take a look and report back if I hit any issues.  
> **username_0**: things seem to be working! Thank you so much for all of your help this. Where in the can I learn more about things like "getParsedConent()" and some more of these core concepts that I can't seem to easily find documentation on. The Fractal docs help a little but I don't want to keep pestering you all :)  

---

### Answer by username_1 on 2021-02-26. Score: 1

It is possible with a foreach, something like this:
'transformer' => function(Entry $entry) {
   $values = [];
   foreach($entry->fieldHandle as $block){
    $values[] = $block['fieldMatrixHandle'];
   }
   return [
     'data' => $values,
   ];
 },


---

