mirror of
https://github.com/UndefinedOffset/SortableGridField.git
synced 2024-10-22 17:05:38 +02:00
53eef90c97
Updated examples to say they are written with 3.0.x in mind and public statics should be private in 3.1.x
38 lines
1.4 KiB
Markdown
38 lines
1.4 KiB
Markdown
ModelAdmin implementation Example
|
|
=================
|
|
Please note this example is written with 3.0.x in mind, if you are using 3.1.x make sure you scope all static properties to private not public.
|
|
```php
|
|
/**** MyModelAdmin.php ****/
|
|
class MyModelAdmin extends ModelAdmin {
|
|
public static $menu_title='My Model Admin';
|
|
public static $url_segment='my-model-admin';
|
|
|
|
public static $managed_models=array(
|
|
'MATestObject'
|
|
);
|
|
|
|
public function getEditForm($id = null, $fields = null) {
|
|
$form=parent::getEditForm($id, $fields);
|
|
|
|
//This check is simply to ensure you are on the managed model you want adjust accordingly
|
|
if($this->modelClass=='MATestObject' && $gridField=$form->Fields()->dataFieldByName($this->sanitiseClassName($this->modelClass))) {
|
|
//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'));
|
|
}
|
|
}
|
|
|
|
return $form;
|
|
}
|
|
}
|
|
|
|
/**** MATestObject.php ****/
|
|
class MATestObject extends DataObject {
|
|
public static $db=array(
|
|
'Title'=>'Varchar',
|
|
'SortOrder'=>'Int'
|
|
);
|
|
|
|
public static $default_sort='SortOrder';
|
|
}
|
|
``` |