1

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);
1

I do believe you're missing the handle to the top-level Matrix field when you're passing into setContentFromPost. You should be doing:

$couponCodeMatrix->setContentFromPost(array('couponCodes', $newMatrixData));

Otherwise, you're just passing:

'45623':
    'type': 'blockTypeHandle'
    'enabled': true,
    'fields': []

to the global set, instead of:

'couponCodes':
    '45623':
        'type': 'blockTypeHandle'
        'enabled': true,
        'fields': []

To deal with fetching even disabled blocks, you'll need to actually query the Matrix field's block directly, rather than looking just at the Global Sets content.

$criteria = craft()->elements->getCriteria(ElementType::MatrixBlock);
$criteria->fieldId = craft()->fields->getFieldByHandle('couponCodes')->id;
$criteria->ownerId = $couponCodeMatrix->id; // This is your Global Set id
$criteria->status = null;
$blocks = $criteria->find();

foreach($blocks as $block) { }

More complete code for anyone interested. We loop through the matrix and recreate it only if we updated the disabled status on the matching item.

Thanks again!

    $couponCodeMatrix = craft()->globals->getSetByHandle('couponCodes');

    $criteria = craft()->elements->getCriteria(ElementType::MatrixBlock);
    $criteria->fieldId = craft()->fields->getFieldByHandle('couponCodes')->id;
    $criteria->ownerId = $couponCodeMatrix->id; // This is your Global Set id
    $criteria->status = null;
    $matrixBlocks = $criteria->find();

    $b_updateMatrix = false;
    $o_foundCoupon = false;
    $newMatrixData = array('couponCodes'=>array());
    foreach($matrixBlocks as $block){

        $b_enabled = $block->enabled;
        if ($this->couponCheckMatch($s_code, $block)) {
            $o_foundCoupon = $block;
            if($block->singleUse==1){
                $b_updateMatrix=true;
                $b_enabled = false;
            }
        }

        $blockData = array(
            'type' => $block->getType()->handle,
            'enabled' => $b_enabled,
            'fields' => array(
                'couponCode' => $block->couponCode,
                'couponType' => $block->couponType,
                'couponValue' => $block->couponValue,
                'expiryDate' => $block->expiryDate,
                'singleUse' => $block->singleUse,
            )
        );
        $newMatrixData['couponCodes'][$block->id] = $blockData;
    }
    if($b_updateMatrix) {
        $couponCodeMatrix->setContentFromPost($newMatrixData);
        craft()->globals->saveContent($couponCodeMatrix);
    }  
| improve this answer | |
  • Oh... I was so close. That worked perfect. – Walker Designs Jan 19 '16 at 4:40
  • Interestingly when I call ​​ $couponCodeMatrix = craft()->globals->getSetByHandle('couponCodes'); it returns only those matrix entries that are enabled (hence whenever I run this script now it will delete anything which is not enabled). I can work around this but is there a better way to grab all rows in the matrix including disabled ones? – Walker Designs Jan 19 '16 at 4:42
  • See my updated answer – crawf Jan 19 '16 at 6:03

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.