Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Forum Module /

Discuss the Forum Module.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Customizing the forum registration form


Go to End


4 Posts   3234 Views

Avatar
Yokelassence

Community Member, 15 Posts

8 August 2012 at 10:04pm

Hi guys, just looking for simple advice here.

I have an existing registration form on my website that records some personal details and some misc details of their own choosing.

I want to integrate my existing registration form with the forum module registration form so that when users register with the website, they also get a forum account at the same time.

So either I extend the forum registration form to include the other details I need, or I make my existing registration form create it's own forum user objects

Where should I begin and what sort of things should I be aware of before modifying the forum module registration form?

I know that the forum registration code is inside ForumMemberProfile.php, can I just append to this without consequence?

Avatar
Willr

Forum Moderator, 5523 Posts

12 August 2012 at 12:39pm

Create a DataExtension (or DataObjectDecorator in SS2.4) and apply it to the Member base class.

You can then modify the registration form using the extension hook updateForumFields.

Avatar
facon12

Community Member, 2 Posts

24 August 2012 at 7:30am

Is there documentation anywhere on the Forum module other than: https://github.com/silverstripe/silverstripe-forum. The documentation there is pretty lacking for knowing what hooks and functions to use to extend the module. I realize i could read through the code in the module but it seems like this would be documented in an api or something along those lines. I get the impression from other posts that the forum module had more documentation for previous versions of silverstripe.

Avatar
Yokelassence

Community Member, 15 Posts

9 September 2012 at 8:25pm

Edited: 09/09/2012 8:47pm

Hello again, bumping an old topic I know. But I figured I should give you my results

So I decided not to mess with the original registration form for the forum. I have left it as is in order to avoid creating more problems in the long term. Instead I focused on adding the necessary code to my own registration form so that it has the power to create an object of the Member class

In my own registration form I copied the code from the forum function called doregister which is inside the file called ForumMemberProfile.php and added that to my own code

The key part you need is this:

            $forumGroup = DataObject::get_one('Group', "Code = 'forum-members'");

            if ($member = DataObject::get_one("Member", "`Email` = '" . Convert::raw2sql($data['email']) . "'")) {
                if ($member) {
                    $form->addErrorMessage("Blurb",
                            _t('ForumMemberProfile.EMAILEXISTS', 'Sorry, that email address already exists. Please choose another.'),
                            "bad");

                    // Load errors into session and post back
                    Session::set("FormInfo.Form_RegistrationForm.data", $data);
                    Director::redirectBack();
                    return;
                }
            } elseif ($this->OpenIDAvailable() && ($member = DataObject::get_one("Member",
                            "`IdentityURL` = '" . Convert::raw2sql($data['IdentityURL']) . "'"))) {

                if ($member) {
                    $form->addErrorMessage("Blurb",
                            _t('ForumMemberProfile.OPENIDEXISTS', 'Sorry, that OpenID is already registered. Please choose another or register without OpenID.'),
                            "bad");

                    // Load errors into session and post back
                    Session::set("FormInfo.Form_RegistrationForm.data", $data);
                    Director::redirectBack();
                    return;
                }
            } elseif ($member = DataObject::get_one("Member",
                            "`Nickname` = '" . Convert::raw2sql($data['Nickname']) . "'")) {
                if ($member) {
                    $form->addErrorMessage("Blurb",
                            _t('ForumMemberProfile.NICKNAMEEXISTS', 'Sorry, that nickname already exists. Please choose another.'),
                            "bad");

                    // Load errors into session and post back
                    Session::set("FormInfo.Form_RegistrationForm.data", $data);
                    Director::redirectBack();
                    return;
                }
            }

            $member = Object::create('Member');
            $member->Nickname = $data['Nickname'];
            $member->Firstname = $data['Firstname '];
            $member->Surname = $data['Surname '];
            $member->Occupation = $data['Occupation '];
            $member->Company = $data['Company '];
            $member->City = $data['City '];
            $member->Country = $data['Country '];
            $member->Email = $data['Email '];
            $member->write();
            $member->login();

            $forumGroup->Members()->add($member);

I love how I can call such classes from anywhere within Silverstripe. This works just fine outside of the original forum code so long as my registration form still carries the expected database fields (This is all inside $data)

With that together with all the other dependent code snippets from ForumMemberProfile.php tucked away inside my controller it worked like a charm. Whenever customers sign up with the website they automatically get a forum account with their subscription

BUT I should also add that I did this before I saw Willr's comment

Now having seen Willr's comment, I'm going to suggest that instead. I haven't played with DataObjectDecorator before (nice example here) so it did not cross my mind but that would be the correct way of extending the forum's registration form with new fields.

My method dodges around the problem rather than addresses it, although it does give me my own form which I have full control over