<p>The first code section looks correct, other than that you're first creating a bodyBlocks array and then assigning a string to it, but that's not really consequential and not the issue behind this failing.</p>

<p>Do yourself a favor and set up PHPStorm + xdebug, you'll find the issue very easily. I suspect you might have some mixed up identifiers (field names, block handles) etc.</p>

CC BY-SA 4.0
2019-08-19T09:53:51.573Z
That was exactly it, I was referencing a checkbox field and not the matrix block redactor field. urgh! Thanks for that.
Mark
CC BY-SA 4.0
2019-08-16T15:15:31.660Z
2019-08-16T15:15:31.660Z
Udo
<p>I am trying to grab the contents a matrix field (Redactor).</p>

<p>Here is the section of code I am struggling with:</p>

<pre><code>$bodyBlocks = [];
foreach ($entry-&gt;getFieldValue('pageBuilder')-&gt;all() as $block){
   switch ($block-&gt;type-&gt;handle) {
      case 'blockText':
      $bodyBlocks = $block-&gt;blockTextPara;
      break;
   }
}

// Begin defining the response data for this entry
$data = [
  'title' =&gt; $entry-&gt;title,
  'slug' =&gt; $entry-&gt;url,
  'status' =&gt; $entry-&gt;status,
  'textArea' =&gt; $bodyBlocks,
]; 
</code></pre>

<p>It is submitting to Algolia but the contents of the matrix block is empty. </p>

<pre><code>{
  "title": "Transport",
  "slug": "http://xxxxx/boarding-at-bruern/the-london-link",
  "status": "live",
  "textArea": {},
  "heroBanner": "http://xxxx/images/uploads/banners/_algoliaTransform/ASC7795.jpg",
  "objectID": "1_408"
}
</code></pre>

<p>For reference here is my entire scout.php config file:</p>

<pre><code>&lt;?php

    return [
        "sync" =&gt; true,
        "application_id" =&gt; "xxx",
        "admin_api_key" =&gt; "xxx",
        "mappings" =&gt; [
            [
                'indexName' =&gt; 'xxx',
                'elementType' =&gt; \craft\elements\Entry::class,
                'criteria' =&gt; [
                    'section' =&gt; 'websitePages',
                    'type' =&gt; 'websitePages'
                ],            
                'transformer' =&gt; function(craft\elements\Entry $entry) {

                    Craft::$app-&gt;getConfig()-&gt;getGeneral()-&gt;generateTransformsBeforePageLoad = true;

                    $bodyBlocks = [];
                    foreach ($entry-&gt;getFieldValue('pageBuilder')-&gt;all() as $block){
                        switch ($block-&gt;type-&gt;handle) {
                            case 'blockText':
                                $bodyBlocks = $block-&gt;blockTextPara;
                                break;
                        }
                    }

                    // Begin defining the response data for this entry
                    $data = [
                        'title' =&gt; $entry-&gt;title,
                        'slug' =&gt; $entry-&gt;url,
                        'status' =&gt; $entry-&gt;status,
                        'textArea' =&gt; $bodyBlocks,
                    ];              

                    // Get the first asset in a "heroBanner" Assets field (if there is one) and then add it to the data array.         
                    $heroBanner = $entry-&gt;heroBanner-&gt;one();                
                    $data['heroBanner'] = $heroBanner ? $heroBanner-&gt;getUrl('algoliaTransform') : '/images/site/search-default.jpg';

                    return $data;               

                },
            ],
        ],
    ];
</code></pre>

CC BY-SA 4.0
2019-08-17T10:44:17.847Z
So, what is exactly the output from `$bodyBlocks` if you `dd()` the variable?
Christopher Dosin
<craft3><element>
Matrix | Craft 3 Element API / Scout Plugin
# Matrix | Craft 3 Element API / Scout Plugin

(Asked by: username_0 on 2019-08-16)

I am trying to grab the contents a matrix field (Redactor).
Here is the section of code I am struggling with:
$bodyBlocks = [];
foreach ($entry->getFieldValue('pageBuilder')->all() as $block){
   switch ($block->type->handle) {
      case 'blockText':
      $bodyBlocks = $block->blockTextPara;
      break;
   }
}

// Begin defining the response data for this entry
$data = [
  'title' => $entry->title,
  'slug' => $entry->url,
  'status' => $entry->status,
  'textArea' => $bodyBlocks,
]; 

It is submitting to Algolia but the contents of the matrix block is empty. 
{
  "title": "Transport",
  "slug": "http://xxxxx/boarding-at-bruern/the-london-link",
  "status": "live",
  "textArea": {},
  "heroBanner": "http://xxxx/images/uploads/banners/_algoliaTransform/ASC7795.jpg",
  "objectID": "1_408"
}

For reference here is my entire scout.php config file:
 true,
        "application_id" => "xxx",
        "admin_api_key" => "xxx",
        "mappings" => [
            [
                'indexName' => 'xxx',
                'elementType' => \craft\elements\Entry::class,
                'criteria' => [
                    'section' => 'websitePages',
                    'type' => 'websitePages'
                ],            
                'transformer' => function(craft\elements\Entry $entry) {

                    Craft::$app->getConfig()->getGeneral()->generateTransformsBeforePageLoad = true;

                    $bodyBlocks = [];
                    foreach ($entry->getFieldValue('pageBuilder')->all() as $block){
                        switch ($block->type->handle) {
                            case 'blockText':
                                $bodyBlocks = $block->blockTextPara;
                                break;
                        }
                    }

                    // Begin defining the response data for this entry
                    $data = [
                        'title' => $entry->title,
                        'slug' => $entry->url,
                        'status' => $entry->status,
                        'textArea' => $bodyBlocks,
                    ];              

                    // Get the first asset in a "heroBanner" Assets field (if there is one) and then add it to the data array.         
                    $heroBanner = $entry->heroBanner->one();                
                    $data['heroBanner'] = $heroBanner ? $heroBanner->getUrl('algoliaTransform') : '/images/site/search-default.jpg';

                    return $data;               

                },
            ],
        ],
    ];

**Tags:** craft3, element

> **Comments:**
> **username_1**: So, what is exactly the output from `$bodyBlocks` if you `dd()` the variable?  

---

## 1 answers

### Answer by username_2 on 2019-08-16. Score: 1 | Accepted answer

The first code section looks correct, other than that you're first creating a bodyBlocks array and then assigning a string to it, but that's not really consequential and not the issue behind this failing.

Do yourself a favor and set up PHPStorm + xdebug, you'll find the issue very easily. I suspect you might have some mixed up identifiers (field names, block handles) etc.

> **Comments:**
> **username_0**: That was exactly it, I was referencing a checkbox field and not the matrix block redactor field. urgh! Thanks for that.  

---

