more docs for GridField & ModelAdmin customization

This commit is contained in:
Zauberfisch 2014-03-10 09:20:44 +00:00
parent 16fe87e4aa
commit 62537c5886
2 changed files with 31 additions and 1 deletions

View File

@ -83,10 +83,17 @@ a new instance of the class to the [api:GridFieldConfig] object. The `GridField`
manipulating the `GridFieldConfig` instance if required. manipulating the `GridFieldConfig` instance if required.
:::php :::php
// option 1: creating a new GridField with the CustomAction
$config = GridFieldConfig::create(); $config = GridFieldConfig::create();
$config->addComponent(new GridFieldCustomAction()); $config->addComponent(new GridFieldCustomAction());
$gridField = new GridField('Teams', 'Teams', $this->Teams(), $config); $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 Now let's go back and dive through the `GridFieldCustomAction` class in more
detail. detail.

View File

@ -160,19 +160,42 @@ For example, we might want to have a checkbox which limits search results to exp
return $list; 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. 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 :::php
class MyAdmin extends ModelAdmin { class MyAdmin extends ModelAdmin {
private static $managed_models = array('Product','Category');
// ... // ...
public function getEditForm($id = null, $fields = null) { public function getEditForm($id = null, $fields = null) {
$form = parent::getEditForm($id, $fields); $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()); $gridField->getConfig()->addComponent(new GridFieldFilterHeader());
return $form; 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 ## Managing Relationships