2

I have a plugin which is used to send a notification email when certain events happen.

I need to watch the contents of a Matrix field attached to a User, and when a Lightswitch field inside the Matrix is 'on', flip another Lightswitch field on the same matrix block. Here's the simplified code I have so far:

craft()->on('users.onBeforeSaveUser', function(Event $event) {
    foreach ($user->getContent()->myMatrixField as $matrixBlock) {
        if($matrixBlock->firstLightswitch){
            $matrixBlock->secondLightswitch = '1';
        }
    }
}

I am getting the following error though:

Property “Craft\MatrixBlockModel.secondLightswitch” is not defined

It's very definitely defined. I presume I'm trying to set the field incorrectly, but can't find out how.

4

You have to do:

$matrixBlock->getContent()->setAttribute('secondLightswitch', '1');

You can only get attributes via magic methods but you can't set them

| improve this answer | |
  • Thanks Robin - now I need to read up about "magic methods". – Matt Wilcox Apr 6 '18 at 8: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.