Jump to:

23486 Posts in 18995 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Extending Member using decorator

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

Page: 1
Go to End
Author Topic: 3460 Views
  • chinaski
    Avatar
    Community Member
    26 Posts

    Extending Member using decorator Link to this post

    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?

  • MateuszU
    Avatar
    Community Member
    89 Posts

    Re: Extending Member using decorator Link to this post

    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.

  • chinaski
    Avatar
    Community Member
    26 Posts

    Re: Extending Member using decorator Link to this post

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

  • MateuszU
    Avatar
    Community Member
    89 Posts

    Re: Extending Member using decorator Link to this post

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

    3460 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.