Question:

Matrix | Craft 3 Element API / Scout Plugin

Savannah: 5 days ago

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:

<?php

    return [
        "sync" => 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;               

                },
            ],
        ],
    ];

Answer:
Matthew: 5 days ago

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.