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.

Data Model Questions

delete on gridfield change to non delete


Reply

3 Posts   268 Views

Avatar
Drumstick

11 February 2014 at 9:47pm Community Member, 20 Posts

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

12 February 2014 at 1:59am (Last edited: 12 February 2014 2:18am), Community Member, 215 Posts

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

12 February 2014 at 8:53am Community Member, 20 Posts

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;
}
}