I don't seem to be able to add a role to a user when they register.
I have a function to return the role as text based on the option that was chosen on the register form. I have checked that this function is returning the correct value by echoing it to the page. If I pass that text into the addRole function (as $myRole) it doesn't work. If I hard code the exact same text into addRole it does work.
Do I need to change my getRole function to return the text in a different format?
function getRole($role) {
// return name of role based on id from register_member_role
switch ($role) {
case 1:
return "type1";
break;
case 2:
return "type2";
break;
case 3:
return "type3";
break;
default:
return "type1";
}
}
$loginRegister = $modules->get('LoginRegister');
$myRole = getRole($input->post('register_member_role'));
wire()->addHookBefore('LoginRegister::createdUser', function($event) {
$u = $event->arguments[0];
$u->addRole($myRole);
$u->save();
});
echo $loginRegister->execute();