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;
},
],
],
];
$bodyBlocks
if youdd()
the variable?