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.

General Questions

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

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

Extending Member using decorator


Reply

4 Posts   3469 Views

Avatar
chinaski

3 January 2010 at 8:04pm Community Member, 26 Posts

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 [url=http://www.silverstripe.org/archive/show/241636#post241636]post[/url]

<?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

5 January 2010 at 12:22pm Community Member, 89 Posts

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

6 January 2010 at 5:11pm Community Member, 26 Posts

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

6 January 2010 at 5:41pm (Last edited: 6 January 2010 5:42pm), Community Member, 89 Posts

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"
));