We are working on a plugin that needs to update the contents of a Matrix field within Globals. I've found little snippets of code but none that do exactly this. Can anyone provide some code or point me to some code that does such? I can't seem to figure it out. Code sample of what I have so far is below. This does not result in any sort of area, but also does not seem to do anything.
As you can see, I'm trying to force all couponCodes
to be 'BBB' whilst copying all other values. Maybe there is a much better way? It would be nice to just update the code value without recreating the full set of data if possible.
$couponCodeMatrix = craft()->globals->getSetByHandle('couponCodes');
$newMatrixData = array();
foreach($couponCodeMatrix->couponCodes as $block) {
$blockData = array(
'type' => $block->getType()->handle,
'enabled' => true,
'fields' => array(
'couponCode' => 'BBB',
'couponType' => $block->couponType,
'couponValue' => $block->couponValue,
'expiryDate' => $block->expiryDate,
'singleUse' => $block->singleUse,
)
);
$newMatrixData[$block->id] = $blockData;
}
$couponCodeMatrix->setContentFromPost($newMatrixData);
craft()->globals->saveContent($couponCodeMatrix);