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

adding new groups via a module


Reply

3 Posts   499 Views

Avatar
Mrfixer

13 April 2012 at 4:23am Community Member, 49 Posts

Hi, im working on a small module (part time hobby) and want it to be so that on install the module will create 3 new member groups, Bronze, Silver, Gold... (they will not have access to admin CMS, just seperate pages at the front end of the site).

The way im hoping to do it is via the modules _config.php file, now i know i can set permissions via GroupDecorator, but how do i make it so that the groups are created on module install and not have to be done via the admin cms >> security tab, i have looked high and low for ideas over the last two days and everything is just returning a blank for me on how i can implement this, does anyone have any ideas on how it can be done.. Thanks

Avatar
Willr

13 April 2012 at 7:00pm Forum Moderator, 5511 Posts

Well there aren't any hooks like onInstall or anything but what most devs do is hook into requireDefaultRecords() on a dataobject. This function is run on every dev/build.

So on a DataObject subclass in your module define something like

function requireDefaultRecords() {
parent::requireDefaultRecords();

if(!DataObject::get('Group', "Code = 'bronze'")) {
$group = new Group();
$group->Code = 'bronze';
$group->Title = "Bronze";
...
$group->write();
}
..
}

Avatar
Mrfixer

19 April 2012 at 3:43am Community Member, 49 Posts

ahh thanks for this Willr, i did spot the requireDefaultRecords() code in group.php before i posted my question, but wasnt too sure if that was the right way to go about it, you cleared up that it is, tyvm..