Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

ENHANCEMENT Addition of ModelAdmin::getExportFields() which allows Mo…

…delAdmin classes to customise which fields should be exported. Defaults to the summary fields from the model class definition.
  • Loading branch information...
commit fca60b786c1c195efb8e35d5ba2c7f3aca521c61 1 parent 4bcd44a
@halkyon halkyon authored
View
16 admin/code/ModelAdmin.php
@@ -115,12 +115,14 @@ public function init() {
function getEditForm($id = null) {
$list = $this->getList();
+ $exportButton = new GridFieldExportButton();
+ $exportButton->setExportColumns($this->getExportFields());
$listField = Object::create('GridField',
$this->modelClass,
false,
$list,
$fieldConfig = GridFieldConfig_RecordEditor::create($this->stat('page_length'))
- ->addComponent(new GridFieldExportButton())
+ ->addComponent($exportButton)
->removeComponentsByType('GridFieldFilterHeader')
);
@@ -146,6 +148,16 @@ function getEditForm($id = null) {
}
/**
+ * Define which fields are used in the {@link getEditForm} GridField export.
+ * By default, it uses the summary fields from the model definition.
+ *
+ * @return array
+ */
+ public function getExportFields() {
+ return singleton($this->modelClass)->summaryFields();
+ }
+
+ /**
* @return SearchContext
*/
public function getSearchContext() {
@@ -414,4 +426,4 @@ static function get_page_length(){
return self::$page_length;
}
-}
+}
View
49 admin/tests/ModelAdminTest.php
@@ -6,6 +6,7 @@ class ModelAdminTest extends FunctionalTest {
protected $extraDataObjects = array(
'ModelAdminTest_Admin',
'ModelAdminTest_Contact',
+ 'ModelAdminTest_Player'
);
function testModelAdminOpens() {
@@ -14,6 +15,22 @@ function testModelAdminOpens() {
$this->assertTrue((bool)Permission::check("ADMIN"));
$this->assertEquals(200, $this->get('ModelAdminTest_Admin')->getStatusCode());
}
+
+ function testExportFieldsDefaultIsSummaryFields() {
+ $admin = new ModelAdminTest_Admin();
+ $admin->modelClass = 'ModelAdminTest_Contact';
+ $this->assertEquals($admin->getExportFields(), singleton('ModelAdminTest_Contact')->summaryFields());
+ }
+
+ function testExportFieldsOverloadedMethod() {
+ $admin = new ModelAdminTest_PlayerAdmin();
+ $admin->modelClass = 'ModelAdminTest_Player';
+ $this->assertEquals($admin->getExportFields(), array(
+ 'Name' => 'Name',
+ 'Position' => 'Position'
+ ));
+ }
+
}
class ModelAdminTest_Admin extends ModelAdmin implements TestOnly {
@@ -23,10 +40,36 @@ class ModelAdminTest_Admin extends ModelAdmin implements TestOnly {
'ModelAdminTest_Contact',
);
}
+class ModelAdminTest_PlayerAdmin extends ModelAdmin implements TestOnly {
+ static $url_segment = 'testadmin';
+
+ public static $managed_models = array(
+ 'ModelAdminTest_Player'
+ );
+ public function getExportFields() {
+ return array(
+ 'Name' => 'Name',
+ 'Position' => 'Position'
+ );
+ }
+}
class ModelAdminTest_Contact extends DataObject implements TestOnly {
static $db = array(
- "Name" => "Varchar",
- "Phone" => "Varchar",
+ 'Name' => 'Varchar',
+ 'Phone' => 'Varchar',
+ );
+ static $summary_fields = array(
+ 'Name' => 'Name',
+ 'Phone' => 'Phone'
+ );
+}
+class ModelAdminTest_Player extends DataObject implements TestOnly {
+ static $db = array(
+ 'Name' => 'Varchar',
+ 'Position' => 'Varchar',
);
-}
+ static $has_one = array(
+ 'Contact' => 'ModelAdminTest_Contact'
+ );
+}
View
23 docs/en/reference/modeladmin.md
@@ -167,26 +167,22 @@ Export is also available, although at the moment only to the CSV format,
through a button at the end of a results list. You can also export search results.
It is handled through the `[api:GridFieldExportButton]` component.
-To customize the exported columns, override the edit form implementation in your `ModelAdmin`:
+To customize the exported columns, create a new method called `getExportFields` in your `ModelAdmin`:
:::php
class MyAdmin extends ModelAdmin {
// ...
- function getEditForm($id = null) {
- $form = parent::getEditForm($id);
- if($this->modelClass == 'Product') {
- $grid = $form->Fields()->fieldByName('Product');
- $grid->getConfig()->getComponentByType('GridFieldExportButton')
- ->setExportColumns(array(
- // Excludes 'ProductCode' from the export
- 'Name' => 'Name',
- 'Price' => 'Price'
- ));
- }
- return $form;
+ function getExportFields() {
+ return array(
+ 'Name' => 'Name',
+ 'ProductCode' => 'Product Code',
+ 'Category.Title' => 'Category'
+ );
}
}
+Dot syntax support allows you to select a field on a related `has_one` object.
+
## Extending existing ModelAdmins
Sometimes you'll work with ModelAdmins from other modules, e.g. the product management
@@ -195,6 +191,7 @@ also another tool at your disposal: The `[api:Extension]` API.
:::php
class MyAdminExtension extends Extension {
+ // ...
function updateEditForm(&$form) {
$form->Fields()->push(/* ... */)
}
Please sign in to comment.
Something went wrong with that request. Please try again.