Modify Registration Form

Permalink
Hi,
I'd like some help modifying the registration form so that new members can select whether they are a customer, wholesaler or supplier.

My PHP knowledge is very poor so I am hoping an addon has been developed that can help or that there is a simple solution that I'm just not seeing due to inexperience.

I have a test site set up and there is a Members Page with specific content for each group.
Advanced permissions are enabled on the site and the page contains 3 blocks where the permissions only allow viewing by the relevant group. Each group only sees their relevant content - content for the other groups is invisible.

I have customer, wholesaler & supplier set up as user groups.
I have manually created 3 users and added each to one of the groups.

Everything seems to be working perfectly so far - when guests go to the page they are redirected to the standard login/register and correct content is displayed when I login with the manually created accounts.

All I need now is for new people registering to be able to select whether they are a customer, supplier or wholesaler when they register.

I've tried searching in addons to see if there is already a custom registration form where I can add checkboxes or a dropdown containing my user groups but can't seem to find anything. I've also searched the forum but no joy there.

Many thanks in advance
:)

AllisonFewtrell
 
mnakalay replied on at Permalink Best Answer Reply
mnakalay
Hello.

From your dashboard, under "Members" go to "Attributes" and create a new attribute using the "Option List" type.

Give it the handle "group_name".
Give it any name you want, that's the label that will appear on the form.

Make sure you check "Show on Registration Form", "Require on Registration Form" and "Hide none option from the list". I also suggest you check "Display full option list when selecting".

DO NOT check "Allow multiple options to be chosen" and "Allow users to add to this list".

All the other options are up to you depending on your needs.

At the bottom, add your values. One value per user group you have.

IMPORTANT: write your values exactly as the user group name is. So if you have a user group named "Editors", add a value of "Editors" to your attribute. Not "editors", not "editor".

Then save your attribute.

Now, still under "Members" go to "User Groups" and create your groups if they don't already exist. Again, exact same names you used as your attribute values.

You then need to edit a file on your server. Look for the file application/bootstrap/app.php

You will see it already contains a bunch of commented code examples.

At the bottom of the file add this code
Events::addListener(
    'on_user_attributes_saved',
    function ($event) {
        $ui = $event->getUserInfoObject();
        if ($ui) {
            $groupName = $ui->getAttribute('group_name');
            $group = Group::getByName($groupName);
            if ($group) {
                $ui->getUserObject()->enterGroup($group);
            }
        }
    }
);

You'll notice that in the code, I am using the exact handle "group_name" I gave my attribute in this line
$groupName = $ui->getAttribute('group_name');

So if you gave your attribute a different handle, make sure to modify the code accordingly.

Then save your file, go to your dashboard and empty your cache and then try to register a new user and it should work.

Be aware that if the same attribute is modified for an existing user later, the user will also be added to the group. It's not just on registration, it's any time that attribute value is changed for a user.

So if you don't want the attribute value to be changed by the user after registration, do not check "Editable in Profile" when creating the attribute.

I hope this helps.
AllisonFewtrell replied on at Permalink Reply 1 Attachment
AllisonFewtrell
Hi mnakalay
Thank you for these clear instructions. I was able to modify the app.php and the options are now appearing on the registration form - perfect!

It's still not quite right though.
I made sure I had cleared the cache after modifying the app.php.
I was able to register a new user (customer2) and selected the Customers option on the form.
When I logged in as admin and looked at customer2 in the members list the user hadn't actually been added to the group, though.

So when I login as customer2 I get a 403 error, because only members of the user-groups can see the landing page that is set up for members.

I've attached a screenshot of customer2 details from the members section.

This is the code I added to app.php my handle is member_types exactly as I set up in the attributes:
Events::addListener(
    'on_user_attributes_saved',
    function ($event) {
        $ui = $event->getUserInfoObject();
        if ($ui) {
            $groupName = $ui->getAttribute('member_types');
            $group = Group::getByName($groupName);
            if ($group) {
                $ui->getUserObject()->enterGroup($group);
            }
        }
    }
);


Any idea where I'm going wrong please?
Many thanks
:)
mnakalay replied on at Permalink Reply
mnakalay
Are you sure you typed the attribute options and the group names exactly the same way? No extra spaces, same capitalization?

If you are sure you did, and that's not the issue then I don't know. I tested that code, and it worked so I'm sure of it.

If you want I can have a look for you. If you send me your login and an ftp access, I can do that on Monday.
jrenza replied on at Permalink Reply
Sorry to barge in on this thread, my problem is similar. I've modified the Register form to include an 'Options List' select/dropdown. However, I need the values to autocomplete/auto-populate from a database table. Which file do I modify in order to add this functionality?