This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

lucas.sanner54070
lucas.sanner54070

In my plugin I need to extend the update function of the User plugin. This is what I've tried so far:

 public function boot()
 {  
     \RainLab\User\Controllers\Users::extend( function($controller) {   
            $controller->addDynamicMethod('update', function($recordId = null, $context = null) use ($controller) {
                 // do stuff
                return $controller->asExtension('FormController')->update($recordId, $context);
             });
       });
  }

But it doesn't work. The function is not called when an admin enters the update form on backend.
Can someone helps me ?

Last updated

mjauvin
mjauvin

This won't work since the method already exists in the class hierarchy.

What exactly are you trying to do?

Look at using a handler for the model.beforeUpdate event maybe?

ref. https://octobercms.com/docs/api/model/beforeupdate

mjauvin
mjauvin

Actually, looking at the source code, addDynamicMethod() can add a new method even if one already exists with the same name, but the behavior is uncertain. I confused with addDynamicProperty()

Last updated

mjauvin
mjauvin

but I'd still look into the event I mentioned above

lucas.sanner54070
lucas.sanner54070

What exactly are you trying to do?
Well, actually what I'd like to do is to check some data when an admin clicks on the Update button then, according to the data, let the admin edit the user form as usual or redirect him to the user list.
The model.beforeUpdate event won't work in this case as the admin is already in the user form.
I was thinking about using routes but I'm not sure it's the proper way to do it.
Any suggestions ?

mjauvin
mjauvin

Oh, you mean the "update details" button?

lucas.sanner54070
lucas.sanner54070

Yes sorry, I meant the "update details" button.

mjauvin
mjauvin

I'll check this and come back to you.

lucas.sanner54070
lucas.sanner54070

Thank you very much ;)

mjauvin
mjauvin

You should be able to use the page.beforeDisplay event to do what you want.

ref. https://octobercms.com/docs/api/backend/page/beforedisplay

mjauvin
mjauvin

Event::listen('backend.page.beforeDisplay', function ($controller, $action, $params) {
   if (! ($controller instanceof 'RainLab\User\Controllers\Users' && $action === 'update') {
      return;
   }
   $user_id = $params['id'];
   $model = $controller->formFindModelObject($user_id);
   [do verifications with model]
});
lucas.sanner54070
lucas.sanner54070

Awesome ! Exactly what I needed.
Thanks.

1-12 of 12

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.