Closed boxadesign closed 5 years ago
Solution if interest to anyone...
'api/globals.json' => function() {
$langHandle = Craft::$app->request->getQueryParam('lang', 'en');
return [
'elementType' => GlobalSet::class,
'criteria' => ['site' => $langHandle],
'transformer' => function(GlobalSet $globalSet) {
$index = -1;
$data = [];
foreach ($globalSet->globals as $block) {
switch ($block->type->handle) {
case 'listHeader':
$index++;
$data[$index]['title'] = $block->listTitle;
$data[$index]['items'] = [];
break;
case 'listItem':
$data[$index]['items'][] = [
'link' => $block->listText,
'url' => $block->listUrl,
];
break;
}
}
return [
'footerLinks' => $data,
];
}
];
}
Hi,
I want to output all globals with one endpoint. This is trivial for the most part but I need to have some objects in a specific shape.
I'm using a matrix block to handle the content. The main issue I have is I need the json for the
footerLinks
array to be:Here is the code I have so far:
And this is the current output:
And this is a screenshot of the matrix block:
Essentially I need to create a new object for each
listHeader
andlistItem
set and also wrap each set oflistItem
objects in an array calleditems
. Currently it doesn't group anything together and outputs each individuallistItem
in an array.Any help would be great.