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

adding new groups via a module


Go to End


3 Posts   2117 Views

Avatar
Mrfixer

Community Member, 49 Posts

13 April 2012 at 4:23am

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

Forum Moderator, 5523 Posts

13 April 2012 at 7:00pm

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

Community Member, 49 Posts

19 April 2012 at 3:43am

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