cb2004 Posted January 21, 2019 Share Posted January 21, 2019 I am having a lot of fun with the users in different parents/templates: https://processwire.com/blog/posts/processwire-core-updates-2.5.14/#multiple-templates-or-parents-for-users What I would like to do is when a user is added using my new user template it should automatically add the role, this field can then be hidden. I can get the hook to work in ready.php using 'saveReady' but not 'added'. Any ideas? $this->addHookAfter('Users::saveReady', function($event) { $user = $event->arguments(0); if($user->template->id === 46) { if($user->get('_is_saving')) return; $user->setQuietly('_is_saving', true); $user->addRole('member'); $user->save(); $user->setQuietly('_is_saving', false); } }); Link to comment Share on other sites More sharing options...
adrian Posted January 21, 2019 Share Posted January 21, 2019 There is no direct Users::added method. Maybe you can use Pages::added instead and check that the template is the user template. I feel like Users should inherit Pages::added, but it doesn't seem to work, but Pages::added does. 2 Link to comment Share on other sites More sharing options...
adrian Posted January 21, 2019 Share Posted January 21, 2019 BTW, I know that https://processwire.com/api/ref/users/added/ shows that Users:added is hookable. Interestingly, I don't think there is actually a link to this page anywhere, certainly not from here: https://processwire.com/api/ref/users/ where you would think there would be one. I think there still needs to be some work on the docs for stuff like this. @ryan ? 2 Link to comment Share on other sites More sharing options...
cb2004 Posted January 21, 2019 Author Share Posted January 21, 2019 36 minutes ago, adrian said: BTW, I know that https://processwire.com/api/ref/users/added/ shows that Users:added is hookable. Interestingly, I don't think there is actually a link to this page anywhere, certainly not from here: https://processwire.com/api/ref/users/ where you would think there would be one. I think there still needs to be some work on the docs for stuff like this. @ryan ? Yep, that's why I was expecting it to work and banging my head against the table when it didn't. I tried hooking into Pages::added but: Method Page::addRole does not exist or is not callable in this context Link to comment Share on other sites More sharing options...
bernhard Posted January 21, 2019 Share Posted January 21, 2019 18 minutes ago, cb2004 said: Method Page::addRole does not exist or is not callable in this context I guess when you hook Pages::added you need to call addRole() on the user object, not on the page object, so you'd need to convert that page from the hookevent into a user: $wire->addHookAfter('Pages::added(template=user)', function(HookEvent $e) { $user = $this->users->get($e->arguments(0)->id); $user->addRole(...); $user->save(); }); Not tested ? 2 Link to comment Share on other sites More sharing options...
adrian Posted January 21, 2019 Share Posted January 21, 2019 There are lots of issues with Roles and Permissions hooks as well: https://github.com/processwire/processwire-issues/issues/360 Perhaps it would be worth commenting on that issue that User hooks also have the same problem. These really need to be fixed. 2 Link to comment Share on other sites More sharing options...
cb2004 Posted January 21, 2019 Author Share Posted January 21, 2019 6 minutes ago, bernhard said: I guess when you hook Pages::added you need to call addRole() on the user object, not on the page object, so you'd need to convert that page from the hookevent into a user: $wire->addHookAfter('Pages::added(template=user)', function(HookEvent $e) { $user = $this->users->get($e->arguments(0)->id); $user->addRole(...); $user->save(); }); Not tested ? Nice thinking Bernhard but no joy. In my case I may have to get users added through the frontend anyway. Creating a name for a user is an unnecessary step for me so people will be adding an email address for the name, and then when the user is added they will be adding the email address again to the email field. Off topic I know but just in case I revisit this thread in years to come. 5 minutes ago, adrian said: There are lots of issues with Roles and Permissions hooks as well: https://github.com/processwire/processwire-issues/issues/360 Perhaps it would be worth commenting on that issue that User hooks also have the same problem. These really need to be fixed. I will do that, cheers. Link to comment Share on other sites More sharing options...
bernhard Posted January 21, 2019 Share Posted January 21, 2019 2 minutes ago, cb2004 said: Nice thinking Bernhard but no joy I just tested it and it worked as expected ? // ready.php $wire->addHookAfter('Pages::added(template=user)', function(HookEvent $e) { $user = $this->users->get($e->arguments(0)->id); $user->addRole('superuser'); $user->save(); }); Tried it in the backend by adding a user manually. Maybe you added your user via API? 4 Link to comment Share on other sites More sharing options...
cb2004 Posted January 21, 2019 Author Share Posted January 21, 2019 8 minutes ago, bernhard said: I just tested it and it worked as expected ? // ready.php $wire->addHookAfter('Pages::added(template=user)', function(HookEvent $e) { $user = $this->users->get($e->arguments(0)->id); $user->addRole('superuser'); $user->save(); }); Tried it in the backend by adding a user manually. Maybe you added your user via API? Yep, my bad. awesome job on that one. I just had to change 'template=user' to 'template=46' or 'template=members-detail' as my users are not within the normal section for this site. 2 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now