diff --git a/admin/code/ModelAdmin.php b/admin/code/ModelAdmin.php index d90201f31..1c5a1834c 100644 --- a/admin/code/ModelAdmin.php +++ b/admin/code/ModelAdmin.php @@ -115,12 +115,14 @@ abstract class ModelAdmin extends LeftAndMain { 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') ); @@ -145,6 +147,16 @@ abstract class ModelAdmin extends LeftAndMain { return $form; } + /** + * 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 */ @@ -414,4 +426,4 @@ abstract class ModelAdmin extends LeftAndMain { return self::$page_length; } -} \ No newline at end of file +} diff --git a/admin/tests/ModelAdminTest.php b/admin/tests/ModelAdminTest.php index 288912083..19ee1456f 100644 --- a/admin/tests/ModelAdminTest.php +++ b/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 @@ class ModelAdminTest extends FunctionalTest { $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', ); -} \ No newline at end of file + 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' + ); +} diff --git a/docs/en/reference/modeladmin.md b/docs/en/reference/modeladmin.md index bf439564c..d934fd60c 100644 --- a/docs/en/reference/modeladmin.md +++ b/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(/* ... */) }