Created
May 3, 2017 07:43
-
-
Save wernerkrauss/e49898de328ec469877bb32e202c6028 to your computer and use it in GitHub Desktop.
a ModelAdmin with sortable gridfield (if the managed model has a field "SortOrder")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MySortableAdmin extends ModelAdmin | |
{ | |
private static $managed_models = [ | |
'MySortableDataObject' | |
]; | |
private static $url_segment = "sorable-demo"; | |
private static $menu_title = "Sort stuff"; | |
public $showImportForm = false; | |
public function getEditForm($id = null, $fields = null) { | |
$form=parent::getEditForm($id, $fields); | |
$model = singleton($this->modelClass); | |
/** add sorting if we have a field for... */ | |
if (class_exists('GridFieldSortableRows') | |
&& $model->hasField('SortOrder') | |
&& $gridField=$form->Fields()->dataFieldByName($this->sanitiseClassName($this->modelClass))) { | |
if($gridField instanceof GridField) { | |
$gridField->getConfig()->addComponent(new GridFieldSortableRows('SortOrder')); | |
} | |
} | |
return $form; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment