From 4bcd44aee6f09f4efd0ad6de9aa37466e96d3894 Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Sun, 25 Mar 2012 19:54:38 +1300 Subject: [PATCH 1/2] ENHANCEMENT GridFieldExportButton now supports dot syntax for column sources through DataObject::relField() --- forms/gridfield/GridFieldExportButton.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/forms/gridfield/GridFieldExportButton.php b/forms/gridfield/GridFieldExportButton.php index 021bdecb3..919094196 100644 --- a/forms/gridfield/GridFieldExportButton.php +++ b/forms/gridfield/GridFieldExportButton.php @@ -113,7 +113,7 @@ class GridFieldExportButton implements GridField_HTMLProvider, GridField_ActionP foreach($items as $item) { $columnData = array(); foreach($csvColumns as $columnSource => $columnHeader) { - $value = $item->$columnSource; + $value = $item->relField($columnSource); $value = str_replace(array("\r", "\n"), "\n", $value); $columnData[] = '"' . str_replace('"', '\"', $value) . '"'; } @@ -172,4 +172,4 @@ class GridFieldExportButton implements GridField_HTMLProvider, GridField_ActionP } -} \ No newline at end of file +} From fca60b786c1c195efb8e35d5ba2c7f3aca521c61 Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Sun, 25 Mar 2012 20:01:43 +1300 Subject: [PATCH 2/2] ENHANCEMENT Addition of ModelAdmin::getExportFields() which allows ModelAdmin classes to customise which fields should be exported. Defaults to the summary fields from the model class definition. --- admin/code/ModelAdmin.php | 16 +++++++++-- admin/tests/ModelAdminTest.php | 49 +++++++++++++++++++++++++++++++-- docs/en/reference/modeladmin.md | 23 +++++++--------- 3 files changed, 70 insertions(+), 18 deletions(-) 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(/* ... */) }