Skip to content

Instantly share code, notes, and snippets.

@wernerkrauss
Created May 3, 2017 07:43
Show Gist options
  • Save wernerkrauss/e49898de328ec469877bb32e202c6028 to your computer and use it in GitHub Desktop.
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")
<?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