1

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;               

                },
            ],
        ],
    ];
  • So, what is exactly the output from $bodyBlocks if you dd() the variable? – Christopher Dosin Aug 17 '19 at 10:44
1

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.

  • That was exactly it, I was referencing a checkbox field and not the matrix block redactor field. urgh! Thanks for that. – Mark Aug 19 '19 at 9:53

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.