From b21c69d81447282dc79723a01181cf7fdb507618 Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Mon, 25 Nov 2019 12:41:24 +1300 Subject: [PATCH] DOC Explain how to use the new protected getGridField and getGridFieldConfig methods on ModelAdmin --- .../01_ModelAdmin.md | 128 +++++++++++++++--- 1 file changed, 108 insertions(+), 20 deletions(-) diff --git a/docs/en/02_Developer_Guides/15_Customising_the_Admin_Interface/01_ModelAdmin.md b/docs/en/02_Developer_Guides/15_Customising_the_Admin_Interface/01_ModelAdmin.md index 6c9c14253..cb496f952 100644 --- a/docs/en/02_Developer_Guides/15_Customising_the_Admin_Interface/01_ModelAdmin.md +++ b/docs/en/02_Developer_Guides/15_Customising_the_Admin_Interface/01_ModelAdmin.md @@ -213,6 +213,7 @@ For example, we might want to exclude all products without prices in our sample ```php +addComponent(new GridFieldFilterHeader()); - $gridFieldName = $this->sanitiseClassName($this->modelClass); - $gridField = $form->Fields()->fieldByName($gridFieldName); - - // modify the list view. - $gridField->getConfig()->addComponent(new GridFieldFilterHeader()); - - return $form; + return $config; } } ``` @@ -312,6 +326,77 @@ to only one specific `GridField`: ```php +modelClass === Product::class) { + $config->addComponent(new GridFieldFilterHeader()); + } + + return $config; + } +} +``` + +### Using an extension to customise a ModelAdmin + +You can use an Extension to achieve the same results. Extensions have the advantage of being reusable in many contexts. + +**app/code/ModelAdminExtension.php** + + +```php +addComponent(new GridFieldFilterHeader()); + } +} +``` + +**app/_config/mysite.yml** + +```yaml +MyAdmin: + extensions: + - ModelAdminExtension +``` + +### Altering a ModelAdmin using only `getEditForm()` + +If you're developing against a version of Silverstripe CMS prior to 4.6, your only option is to override `getEditForm()`. This requires a bit more work to access the GridField and GridFieldConfig instances. + +**app/code/MyAdmin.php** + +```php +sanitiseClassName($this->modelClass); $gridField = $form->Fields()->fieldByName($gridFieldName); - if ($gridField) { - $gridField->getConfig()->addComponent(new GridFieldFilterHeader()); - } + // modify the list view. + $gridField->getConfig()->addComponent(new GridFieldFilterHeader()); return $form; }