From 4ead89a4d0d90cffc0a8891fb8aca2165ab0989d Mon Sep 17 00:00:00 2001 From: Simon Welsh Date: Thu, 6 Sep 2012 23:42:48 +1200 Subject: [PATCH] API Adds getManipulatedList() method to GridField to get the SS_List after applying Manipulators --- forms/gridfield/GridField.php | 22 ++++++++++++++++------ tests/forms/GridFieldTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/forms/gridfield/GridField.php b/forms/gridfield/GridField.php index 59f16c803..451ec0834 100644 --- a/forms/gridfield/GridField.php +++ b/forms/gridfield/GridField.php @@ -192,6 +192,21 @@ class GridField extends FormField { public function getList() { return $this->list; } + + /** + * Get the datasource after applying the {@link GridField_DataManipulator}s to it. + * + * @return SS_List + */ + public function getManipulatedList() { + $list = $this->getList(); + foreach($this->getComponents() as $item) { + if($item instanceof GridField_DataManipulator) { + $list = $item->getManipulatedData($this, $list); + } + } + return $list; + } /** * Get the current GridState_Data or the GridState @@ -227,12 +242,7 @@ class GridField extends FormField { $columns = $this->getColumns(); // Get data - $list = $this->getList(); - foreach($this->getComponents() as $item) { - if($item instanceof GridField_DataManipulator) { - $list = $item->getManipulatedData($this, $list); - } - } + $list = $this->getManipulatedList(); // Render headers, footers, etc $content = array( diff --git a/tests/forms/GridFieldTest.php b/tests/forms/GridFieldTest.php index c718c13be..5479e4da2 100644 --- a/tests/forms/GridFieldTest.php +++ b/tests/forms/GridFieldTest.php @@ -392,6 +392,22 @@ class GridFieldTest extends SapphireTest { $this->assertEquals((string)$members[1]->td[0], 'Otto Fischer', 'Second object Name should be Otto Fischer'); $this->assertEquals((string)$members[1]->td[1], 'otto.fischer@example.org', 'Second object Email should be otto.fischer@example.org'); } + + public function testChainedDataManipulators() { + $config = new GridFieldConfig(); + $data = new ArrayList(array(1, 2, 3, 4, 5, 6)); + $gridField = new GridField('testfield', 'testfield', $data, $config); + $endList = $gridField->getManipulatedList(); + $this->assertEquals($endList->Count(), 6); + + $config->addComponent(new GridFieldTest_Component2); + $endList = $gridField->getManipulatedList(); + $this->assertEquals($endList->Count(), 12); + + $config->addComponent(new GridFieldPaginator(10)); + $endList = $gridField->getManipulatedList(); + $this->assertEquals($endList->Count(), 10); + } } class GridFieldTest_Component implements GridField_ColumnProvider, GridField_ActionProvider, TestOnly{ @@ -430,6 +446,14 @@ class GridFieldTest_Component implements GridField_ColumnProvider, GridField_Act } +class GridFieldTest_Component2 implements GridField_DataManipulator, TestOnly { + function getManipulatedData(GridField $gridField, SS_List $dataList) { + $dataList = clone $dataList; + $dataList->merge(new ArrayList(array(7, 8, 9, 10, 11, 12))); + return $dataList; + } +} + class GridFieldTest_Team extends DataObject implements TestOnly { static $db = array( 'Name' => 'Varchar',