Jump to:

23355 Posts in 17838 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » ModelAdmin with Subsite?!

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: 691 Views
  • SSadmin
    Avatar
    Community Member
    90 Posts

    ModelAdmin with Subsite?! Link to this post

    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

  • dacar
    Avatar
    Community Member
    168 Posts

    Re: ModelAdmin with Subsite?! Link to this post

    Same here, has anybody a solution for that?

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: ModelAdmin with Subsite?! Link to this post

    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

  • Adrexia
    Avatar
    Moderator
    3 Posts

    Re: ModelAdmin with Subsite?! Link to this post

    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.

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