Jump to:

5514 Posts in 1733 Topics by 1219 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Read-only ModelAdmin

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 315 Views
  • cybesun
    Avatar
    Community Member
    5 Posts

    Read-only ModelAdmin Link to this post

    Hello,

    I have a simple dataobject like this:

    class MyObject extends DataObject {

    static $db = array(
    'Creation' => 'Date'
    );
    }

    I'd like to have two Model-Admins for the same dataobject.
    One should be a normal ModelAdmin alll objects where Creation is in the future.
    Another should all objects where Creation is in the past and it should show them as read-only.

    I know I can set permission on the MyObject, but that would not solve my problem to be able to have read-only and read-write ModelAdmin.

    How is the approach in silverstripe for such cases ?

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: Read-only ModelAdmin Link to this post

    You need to overwrite ModelAdmin->getEditForm()

    class ReadonlyAdmin extends ModelAdmin {
       public static $managed_models = array(
          'MyObject',
       );

       public function getEditForm($id = null, $fields = null) {
          $form = parent::getEditForm($id, $fields);

          // get gridfield
          $gridfield = $form->Fields()
                ->dataFieldByName($this->sanitiseClassName($this->modelClass));

          $gridfieldConfig = $gridfield->getConfig();

          // remove delete & edit buttons
          $gridfieldConfig
                ->removeComponentsByType('GridFieldDeleteAction')
                ->removeComponentsByType('GridFieldEditButton');

          // add a view button
          $gridfieldConfig
                ->addComponent(new GridFieldViewButton());

          return $form;
       }
    }

    However, this is not really secure. You still need to set proper permissions in your DataObject.

  • cybesun
    Avatar
    Community Member
    5 Posts

    Re: Read-only ModelAdmin Link to this post

    Thanks a lot. That was what I was looking for

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