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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Extending Member using decorator


Go to End


4 Posts   4162 Views

Avatar
chinaski

Community Member, 26 Posts

3 January 2010 at 8:04pm

Hello.

I'm quite the noob with ss so please bear with me.

I have extended the member class using a form decorator as per this post

<?php 
class ResidentMemberExt extends DataObjectDecorator {
    function extraDBFields()
    {
        return array(
        'db' => array(
        'MemberType' => "Enum('Resident,Member,Associate,NonResident','Resident')",
        'IsBoardMember' => "Enum('Y,N','N')",
        ),
        );
    }
    public function updateCMSFields(FieldSet &$fields) {
        $fields->push(new DropdownField('MemberType', 'Member Type', $this->owner->dbObject('MemberType')->enumValues()));
        $fields->push(new DropdownField('IsBoardMember', 'Is a Board Member', $this->owner->dbObject('IsBoardMember')->enumValues()));
    }
}

Now I want to be able to add new members using the new cms fields via the cms admin panel. How to accomplish this?

Avatar
MateuszU

Community Member, 89 Posts

5 January 2010 at 12:22pm

If you apply this decorator, it will stick to the Member class, and you will have all the members decorated (even the old ones). You can't instantiate the decorator itself. If you want only some members to have the special "abilities" you have to subclass the Member instead of decorating.

Also don't forget to apply the decorator:

DataObject::add_extension('Member', 'ResidentMemberExt');

And run dev/build, this will create appropriate fields on the Members in the database.

Avatar
chinaski

Community Member, 26 Posts

6 January 2010 at 5:11pm

Thanks for your reply. I failed to mention in my first post that I put the DataObject::add_extension('Member', 'ResidentMemberExt'); line into the config file and loaded the build url. The members table is populated with the new fields.

Given that I've updated the cms fields in the decorator, I want to know something which seems basic: how do I start adding new members via a form that is aware of the new fields? How do I create the admin interface (or can the I get at via the security tab somehow?)

Avatar
MateuszU

Community Member, 89 Posts

6 January 2010 at 5:41pm

Edited: 06/01/2010 5:42pm

Good question. Have a look at the Forum module, it does the same thing you want to accomplish. Here is excerpt from the forum/code/ForumRole.php:

	function updateCMSFields(FieldSet &$fields) {
		if(Permission::checkMember($this->owner->ID, "ACCESS_FORUM")) {
			$fields->addFieldToTab('Root.Forum',new TextField("Nickname", "Nickname"), "FirstName");
			$fields->addFieldToTab('Root.Forum',new TextField("Occupation", "Occupation"), "Surname");
			$fields->addFieldToTab('Root.Forum',new CountryDropdownField("Country", "Country"), "Occupation");
			$fields->addFieldToTab('Root.Forum',new ImageField("Avatar", "Upload avatar."));
			$fields->addFieldToTab('Root.Forum',new DropdownField("ForumRank", "User rating", array(
				"Community Member" => _t('ForumRole.COMMEMBER'),
				"Administrator" => _t('ForumRole.ADMIN','Administrator'),
				"Moderator" => _t('ForumRole.MOD','Moderator')
			)));
		}
	}

So change the $fields->push to $fields->addFieldToTab and see what it brings.

You may also want to add additional columns into the overview table, add something along these lines to the mysite/_config.php:

MemberTableField::addMembershipFields( array(
	"Nickname" => "Nickname",
	"Occupation" => "Occupation",
	"Country" => "Country",
	"ForumRank" => "ForumRank"
));