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

ModelAdmin with Subsite?!


Go to End


4 Posts   2361 Views

Avatar
SSadmin

Community Member, 90 Posts

26 January 2011 at 10:31am

Hey, guys

Currently, i am working on a project that requires using subsite moudel and modeladmin.

I just wonder is that possible to only see the current subsite data in module admin tab.

The logic is:
one subsite admin(not global admin) login to their own subsite, and click on MyModelAdmin Tab on the top CMS navigation, then it will be able to generate the data of managed_models with in current Subsite.

I have looked in ModelAdmin.php, but not sure how could i add the subsite restriction code in?!
Any ideas?

Thanks in advanced.

Alex

Avatar
dacar

Community Member, 173 Posts

7 June 2013 at 1:24am

Same here, has anybody a solution for that?

Avatar
swaiba

Forum Moderator, 1899 Posts

7 June 2013 at 3:44am

Hi,

I don't have an out of the box solution, however I'd advise that you do something akin to translatable.

1) decorate all your dataobjects to have the (hidden) field for the relevant subsite
2) agument all SQL to get those relevant to the current subsite
3) do somethign like https://github.com/silverstripe-labs/silverstripe-translatablemodeladmin to the Model admin

Hope this helps

Avatar
Adrexia

Moderator, 4 Posts

12 September 2013 at 1:09pm

Edited: 12/09/2013 1:14pm

This works for me:

On the model(s)

        private static $has_one = array(
		'Subsite'=>'Subsite'
	);

and...
        public function getCMSFields() {
		$fields = parent::getCMSFields();
		$fields->push(new HiddenField('SubsiteID','SubsiteID', Subsite::currentSubsiteID()));
		return $fields;
	}

On the ModelAdmin:
        public function getEditForm($id = null, $fields = null){
		$form = parent::getEditForm($id, $fields);
		
		$gridField = $form->Fields()->fieldByName($this->sanitiseClassName($this->modelClass));
		
		$list = $gridField->getList()->filter(array('SubsiteID'=>Subsite::currentSubsiteID()));
		$gridField->setList($list);

		return $form;
	}

There might be edge cases I haven't tested for though. And it should probably check for the existence of subsites.