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.

Data Model Questions /

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

Remove/hide delete button from admin edit form section


Go to End


2 Posts   388 Views

Avatar
lulu

Community Member, 7 Posts

6 April 2017 at 2:03am

Hi,

I need to remove 'Delete button' from form edit page in admin panel - like the one highlited on the picture below:

I have following code (PackageBuilderProductController) but it doesn't work, any ideas how to do it?

class PackageBuilderProduct extends DataObject {
     
    public static $db = array(
		'SortOrder' => 'Int',
        'Title' => 'Varchar(128)',
        'Description' => 'Varchar(255)'
    );
 
    static $has_one = array(
        'PackageBuilderPage' => 'PackageBuilderPage'
    );
	
    static $summary_fields = array(
        'Title' => 'Title',
        'Description' => 'Description'
    );
	
    public function getCMSFields() {

		$fields = parent::getCMSFields();
		
		$fields->removeByName("SortOrder");
		
		$fields->addFieldToTab('Root.Main', new TextField('Title', 'Title'));
		$fields->addFieldToTab('Root.Main', new TextField('Description', 'Description'));
		
		return $fields;
		
    }
	
}

class PackageBuilderProductController extends ModelAdmin {

	public function getEditForm($ID = null, $Fields = null)
	{
		$form = parent::getEditForm($ID, $Fields);
		
		$fields = $form->Fields();
		$fields->removeByName('action_doDelete');
		
		return $fields;
	}
	
}

Thanks, L.

Avatar
lulu

Community Member, 7 Posts

6 April 2017 at 8:55pm

Hi,

OK I've solved it now using just canDelete() function, so the code should look like this:

class PackageBuilderProduct extends DataObject {
     
    public static $db = array(
		'SortOrder' => 'Int',
        'Title' => 'Varchar(128)',
        'Description' => 'Varchar(255)'
    );
 
    static $has_one = array(
        'PackageBuilderPage' => 'PackageBuilderPage'
    );
	
    static $summary_fields = array(
        'Title' => 'Title',
        'Description' => 'Description'
    );
	
    public function getCMSFields() {

		$fields = parent::getCMSFields();
		
		$fields->removeByName("SortOrder");
		
		$fields->addFieldToTab('Root.Main', new TextField('Title', 'Title'));
		$fields->addFieldToTab('Root.Main', new TextField('Description', 'Description'));
		
		return $fields;
		
    }

	public function canDelete($member = null) {
		return false;
	}

}

It really hard to find any info on this matter in the internet, it's easy to find information on how to remove delte button from grid field:

removeComponentsByType('GridFieldDeleteAction')

But delete button will still show up on the edit page, above method allows to remove it, hope this will help some more people in the future.

Thanks, L.