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.

Customising the CMS /

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

Read-only ModelAdmin


Go to End


4 Posts   3988 Views

Avatar
cybesun

Community Member, 9 Posts

11 February 2014 at 9:06pm

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

Community Member, 344 Posts

12 February 2014 at 1:13am

Edited: 12/02/2014 1:38am

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

Community Member, 9 Posts

22 March 2014 at 9:38pm

Thanks a lot. That was what I was looking for

Avatar
alex_sm

Community Member, 19 Posts

8 April 2016 at 12:04am

Edited: 08/04/2016 12:34am

I have a question for Delvin.
I also have a simple dataobject:

class Documents extends DataObject {

	private static $db = array(
		'DocType' => 'Text',
		'ApprovalDate' => 'Date',
		'PublicationDate' => 'Date',
		'DocNumber' => 'Text',
		'DocTitle' => 'Text',
		'KeyWords' => 'Text'
	         );

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

Give me please any idea how to customize my ModelAdmin so, that all users could only view all objects, and only owner (user with ID == MemberID) could edit and delete his objects?
As the result I should like to see such picture: [img=" https://yadi.sk/i/o5Nys_szqnPtQ "]
I try to use such code
if (!(Member::currentUserID() ==  $Value_of_MemberID_Field   )) {
    	$gridfieldConfig->removeComponentsByType('GridFieldDeleteAction')
			->removeComponentsByType('GridFieldEditButton');
		// add a view button
		$gridfieldConfig
				->addComponent(new GridFieldViewButton());
} 

How can I get $Value_of_MemberID_Field in the row of GridField?