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.

Customising the CMS

Read-only ModelAdmin


Reply

3 Posts   363 Views

Avatar
cybesun

11 February 2014 at 9:06pm Community Member, 9 Posts

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 ?

Avatar
Devlin

12 February 2014 at 1:13am (Last edited: 12 February 2014 1:38am), Community Member, 215 Posts

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.

Avatar
cybesun

22 March 2014 at 9:38pm Community Member, 9 Posts

Thanks a lot. That was what I was looking for