Jump to:

3431 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » delete on gridfield change to non delete

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

Page: 1
Go to End
Author Topic: 237 Views
  • Drumstick
    Avatar
    Community Member
    20 Posts

    delete on gridfield change to non delete Link to this post

    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.

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: delete on gridfield change to non delete Link to this post

    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

  • Drumstick
    Avatar
    Community Member
    20 Posts

    Re: delete on gridfield change to non delete Link to this post

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

    237 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.