From 62537c5886d73aa6d565843e42044869b414faca Mon Sep 17 00:00:00 2001 From: Zauberfisch Date: Mon, 10 Mar 2014 09:20:44 +0000 Subject: [PATCH] more docs for GridField & ModelAdmin customization --- docs/en/howto/gridfield-rowaction.md | 7 +++++++ docs/en/reference/modeladmin.md | 25 ++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/docs/en/howto/gridfield-rowaction.md b/docs/en/howto/gridfield-rowaction.md index ba7e450a9..32e05b25e 100644 --- a/docs/en/howto/gridfield-rowaction.md +++ b/docs/en/howto/gridfield-rowaction.md @@ -83,10 +83,17 @@ a new instance of the class to the [api:GridFieldConfig] object. The `GridField` manipulating the `GridFieldConfig` instance if required. :::php + // option 1: creating a new GridField with the CustomAction $config = GridFieldConfig::create(); $config->addComponent(new GridFieldCustomAction()); $gridField = new GridField('Teams', 'Teams', $this->Teams(), $config); + + // option 2: adding the CustomAction to an exisitng GridField + $gridField->getConfig()->addComponent(new GridFieldCustomAction()); + +For documentation on adding a Component to a `GridField` created by `ModelAdmin` +please view the [ModelAdmin Reference](/reference/modeladmin#gridfield-customization) section `GridField Customization` Now let's go back and dive through the `GridFieldCustomAction` class in more detail. diff --git a/docs/en/reference/modeladmin.md b/docs/en/reference/modeladmin.md index 4f571e2a9..b6d3477b6 100644 --- a/docs/en/reference/modeladmin.md +++ b/docs/en/reference/modeladmin.md @@ -160,19 +160,42 @@ For example, we might want to have a checkbox which limits search results to exp return $list; } } + +### GridField Customization To alter how the results are displayed (via `[api:GridField]`), you can also overload the `getEditForm()` method. For example, to add a new component. :::php class MyAdmin extends ModelAdmin { + private static $managed_models = array('Product','Category'); // ... public function getEditForm($id = null, $fields = null) { $form = parent::getEditForm($id, $fields); - $gridField = $form->Fields()->fieldByName($this->sanitiseClassName($this->modelClass)); + // $gridFieldName is generated from the ModelClass, eg if the Class 'Product' + // is managed by this ModelAdmin, the GridField for it will also be named 'Product' + $gridFieldName = $this->sanitiseClassName($this->modelClass); + $gridField = $form->Fields()->fieldByName($gridFieldName); $gridField->getConfig()->addComponent(new GridFieldFilterHeader()); return $form; } } + +The above example will add the component to all `GridField`s (of all managed models). Alternatively we can also add it to only one specific `GridField`: + + :::php + class MyAdmin extends ModelAdmin { + private static $managed_models = array('Product','Category'); + // ... + public function getEditForm($id = null, $fields = null) { + $form = parent::getEditForm($id, $fields); + $gridFieldName = 'Product'; + $gridField = $form->Fields()->fieldByName($gridFieldName); + if ($gridField) { + $gridField->getConfig()->addComponent(new GridFieldFilterHeader()); + } + return $form; + } + } ## Managing Relationships