2

I have a matrix block attached to my user, the block handler is children and has one field dob date filed something like this:

fields[children][420][fields][childrenDateOfBirth][date]

The problem is that I would like to update the date right before the user is saved. So I have an envent listener in my plugin

craft()->on('users.onBeforeSaveUser', function(Event $event) {

    $matrixData = array();
    $matrix     = $event->params['user']->getContent()->children;

      foreach ($matrix as $block){

        $blockData = array(
                            'type' => $block->getType()->handle,
                            'enabled' => $block->enabled
                           );   

         $blockData['fields'] = array(
                           'childrenDateOfBirth' => array('date'=>'2000-10-10')
                        );

          $matrixData[$block->id] = $blockData;                                     

  }

 $event->params['user']->getContent()->children = $matrixData;

}
  ...............

And I get this error : Fatal error</b>: Call to a member function getType() on array Is there a way to do this?

  • If you var_dump($block);die(); what is it? Is children the name of the Matrix field or is that the name of a Matrix block type? – Brad Bell Oct 20 '15 at 19:13
  • Thank you Brad for your support, If I understood right, you wanted me to dump foreach ($matrix as $block){ var_dump($block);die(); so the $block seems to be object(Craft\MatrixBlockModel) I was following this example: gist.github.com/brandonkelly/8149062 My guess is in that $matrixData is an array, but $event->params['user']->getContent()->children expects some kind of object with getType() method, but I'm not sure :) – Alex Oct 21 '15 at 9:40
1

I did the trick so here is the code if it can help someone else

craft()->on('users.onBeforeSaveUser', function(Event $event) {

   $matrixData = array();
   $matrix     = $event->params['user']->getContent()->children;

   foreach ($matrix as $block){

      $block->getContent()->childrenDateOfBirth = '2000-10-10';
      $matrixData[$block->id] = $block;                                     

   }

   $event->params['user']->getContent()->children = $matrixData;

}
| improve this answer | |

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.