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