2013-08-24 02:19:35 +02:00
|
|
|
ModelAdmin implementation Example
|
|
|
|
=================
|
2017-11-29 15:05:37 +01:00
|
|
|
|
2013-08-24 02:19:35 +02:00
|
|
|
```php
|
|
|
|
/**** MyModelAdmin.php ****/
|
2017-09-11 16:37:22 +02:00
|
|
|
use SilverStripe\Admin\ModelAdmin;
|
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
|
|
|
use UndefinedOffset\SortableGridField\Forms\GridFieldSortableRows;
|
|
|
|
|
2017-11-29 15:05:37 +01:00
|
|
|
class MyModelAdmin extends ModelAdmin
|
|
|
|
{
|
|
|
|
private static $menu_title = 'My Model Admin';
|
|
|
|
|
|
|
|
private static $url_segment = 'my-model-admin';
|
|
|
|
|
|
|
|
private static $managed_models = [
|
|
|
|
'MATestObject',
|
|
|
|
];
|
|
|
|
|
|
|
|
public function getEditForm($id = null, $fields = null)
|
|
|
|
{
|
|
|
|
$form = parent::getEditForm($id, $fields);
|
|
|
|
|
2013-08-24 02:19:35 +02:00
|
|
|
//This check is simply to ensure you are on the managed model you want adjust accordingly
|
2017-11-29 15:05:37 +01:00
|
|
|
if($this->modelClass == 'MATestObject' && $gridField = $form->Fields()->dataFieldByName($this->sanitiseClassName($this->modelClass))) {
|
2013-08-24 02:19:35 +02:00
|
|
|
//This is just a precaution to ensure we got a GridField from dataFieldByName() which you should have
|
|
|
|
if($gridField instanceof GridField) {
|
|
|
|
$gridField->getConfig()->addComponent(new GridFieldSortableRows('SortOrder'));
|
|
|
|
}
|
|
|
|
}
|
2017-09-11 16:37:22 +02:00
|
|
|
|
2013-08-24 02:19:35 +02:00
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**** MATestObject.php ****/
|
2017-09-11 16:37:22 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
|
2017-11-29 15:05:37 +01:00
|
|
|
class MATestObject extends DataObject
|
|
|
|
{
|
|
|
|
private static $db = [
|
|
|
|
'Title' => 'Varchar',
|
|
|
|
'SortOrder' => 'Int',
|
|
|
|
];
|
2017-09-11 16:37:22 +02:00
|
|
|
|
2017-11-29 15:05:37 +01:00
|
|
|
private static $default_sort = 'SortOrder';
|
2013-08-24 02:19:35 +02:00
|
|
|
}
|
2017-09-11 16:37:22 +02:00
|
|
|
```
|