I am trying to save a SuperTable field that sits within a Matrix field.
I have followed the docs as far as I can but for some reason the SuperTable data is not getting saved.
My steps are as follows:
- Get the SuperTable field id:
$matrixBlocks = Craft::$app->getFields()->getFieldByHandle('eventPerformances')->getBlockTypeFields();
$ticketBlockId = $matrixBlocks[2]['id'];
- Check that the field has content and loop over it to get the array of data:
if ($event['schedules'][0]['performances'][0]['tickets']) {
$ticketsMatrix = array();
$i = 1;
foreach ($event['schedules'][0]['performances'] as $performance)
{
foreach ($performance['tickets'] as $ticket) {
$ticketsMatrix['new'.$i] = array(
'type' => $ticketBlockId,
'enabled' => true,
'fields' => array(
'ticketType' => $ticket['type'],
'minPrice' => $ticket['min_price'],
'maxPrice' => $ticket['max_price'],
'currency' => $ticket['currency'],
),
);
$i++;
}
}
} else {
$ticketsMatrix = '';
}
- Pass this array through when saving the parent Matrix field
$performancesMatrix = array();
$performances = $event['schedules'][0]['performances'];
$i = 1;
foreach ($performances as $performance) {
$performancesMatrix['new'.$i] = array(
'type' => 'performance',
'enabled' => true,
'fields' => array(
'performanceDate' => date_create($performance['ts']),
'duration' => $performance['duration'] / 60,
'tickets' => $ticketsMatrix
),
);
$i++;
}
The performanceDate
and duration
fields get saved as expected but the $ticketsMatrix
data is not saving in the nested SuperTable field.
Any assistance would be appreciated!