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

delete on gridfield change to non delete


Go to End


3 Posts   1822 Views

Avatar
Drumstick

Community Member, 20 Posts

11 February 2014 at 9:47pm

Hi
I try to change the action in the gridfield from delete to keep the data record but change a column in the db to 3.

But in the gridfield list this record shouldn't be anymore.

I tried to change the action, but it still deletes the record.

How can I make it, that it does not delete just change the value of a column in my db but does not show the record anymore on the gridlist. It is not a relation.

Thanks for any help.

Avatar
Devlin

Community Member, 344 Posts

12 February 2014 at 1:59am

Edited: 12/02/2014 2:18am

First: Overload GridFieldDeleteAction->handleAction() to do your thing.

class MyGridFieldDeleteAction extends GridFieldDeleteAction{
	public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
		$item = $gridField->getList()->byID($arguments['RecordID']);
		if(!$item) {
			return;
		}

		if($actionName == 'deleterecord') {
			$item->aColumn = 3;
			$item->write();
		} else {
			parent::handleAction($gridField, $actionName, $arguments, $data);
		}
	}
}

Next: exclude a column with value 3 from gridfield

$list = MyObject::get()->exclude('aColumn', '3');
$gridfield = new GridField('list', 'My list', $list);

Last: remove the deletebutton and add mydeletebutton

$gridfield->getConfig()
	->removeComponentsByType('GridFieldDeleteAction')
	->addComponent(new MyGridFieldDeleteAction())

http://doc.silverstripe.org/framework/en/reference/grid-field

Avatar
Drumstick

Community Member, 20 Posts

12 February 2014 at 8:53am

Hi Devlin

thanks a lot for the tipp, it helped me. acutally I had to do this:

<?php

class CustomerAdmin extends ModelAdmin {

public static $managed_models = array (
'Customer',
'Customerinfo'
);
public static $url_segment = 'customerlist';
public static $menu_title = "customerlist";

public function getEditForm($id = null, $fields = null) {
$form = parent::getEditForm($id, $fields);
$gridField = $form->Fields()->fieldByName($this->sanitiseClassName($this->modelClass));
$gridField->getConfig()->addComponent(new GridFieldFilterHeader());
$gridField->getConfig()->removeComponentsByType('GridFieldDeleteAction');
$gridField->getConfig()->addComponent(new MyGridFieldDeleteAction());

return $form;
}

//the record is with this function automatically removed, it's defined so from the cms
public function getList() {
$list = parent::getList();
if($this->modelClass == 'Customer') {
$list->exclude('Sync', '3');
}
return $list;
}
}