From 5fe86be3115c627a21fd64aeeaa483ffc322f236 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Mon, 30 Jan 2012 15:47:22 +0100 Subject: [PATCH 1/2] ENHANCEMENT Added GridFieldConfig->getComponentsByType() and getComponentByType() --- forms/gridfield/GridFieldConfig.php | 31 +++++++- tests/forms/gridfield/GridFieldConfigTest.php | 76 +++++++++++++++++++ 2 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 tests/forms/gridfield/GridFieldConfigTest.php diff --git a/forms/gridfield/GridFieldConfig.php b/forms/gridfield/GridFieldConfig.php index 686d91c3c..33b4cd5f9 100755 --- a/forms/gridfield/GridFieldConfig.php +++ b/forms/gridfield/GridFieldConfig.php @@ -23,7 +23,7 @@ class GridFieldConfig { * */ public function __construct() { - ; + $this->components = new ArrayList(); } public function addComponent(GridFieldComponent $component) { @@ -32,8 +32,7 @@ class GridFieldConfig { } /** - * - * @return ArrayList + * @return ArrayList Of GridFieldComponent */ public function getComponents() { if(!$this->components) { @@ -41,6 +40,32 @@ class GridFieldConfig { } return $this->components; } + + /** + * Returns all components extending a certain class, or implementing a certain interface. + * + * @param String Class name or interface + * @return ArrayList Of GridFieldComponent + */ + public function getComponentsByType($type) { + $components = new ArrayList(); + foreach($this->components as $component) { + if($component instanceof $type) $components->push($component); + } + return $components; + } + + /** + * Returns the first available component with the given class or interface. + * + * @param String ClassName + * @return GridFieldComponent + */ + public function getComponentByType($type) { + foreach($this->components as $component) { + if($component instanceof $type) return $component; + } + } } class GridFieldConfig_Base extends GridFieldConfig { diff --git a/tests/forms/gridfield/GridFieldConfigTest.php b/tests/forms/gridfield/GridFieldConfigTest.php new file mode 100644 index 000000000..d1491fbc2 --- /dev/null +++ b/tests/forms/gridfield/GridFieldConfigTest.php @@ -0,0 +1,76 @@ +assertType('ArrayList', $config->getComponents()); + $this->assertEquals($config->getComponents()->Count(), 0); + + $config + ->addComponent($c1 = new GridFieldConfigTest_MyComponent()) + ->addComponent($c2 = new GridFieldConfigTest_MyOtherComponent()) + ->addComponent($c3 = new GridFieldConfigTest_MyOtherComponent()); + + $this->assertEquals( + new ArrayList(array($c1, $c2, $c3)), + $config->getComponents() + ); + } + + function testGetComponentsByType() { + $config = GridFieldConfig::create() + ->addComponent($c1 = new GridFieldConfigTest_MyComponent()) + ->addComponent($c2 = new GridFieldConfigTest_MyOtherComponent()) + ->addComponent($c3 = new GridFieldConfigTest_MyOtherComponent()); + + $this->assertEquals( + new ArrayList(array($c1)), + $config->getComponentsByType('GridFieldConfigTest_MyComponent') + ); + $this->assertEquals( + new ArrayList(array($c2, $c3)), + $config->getComponentsByType('GridFieldConfigTest_MyOtherComponent') + ); + $this->assertEquals( + new ArrayList(array($c1, $c2, $c3)), + $config->getComponentsByType('GridField_URLHandler') + ); + $this->assertEquals( + new ArrayList(), + $config->getComponentsByType('GridFieldConfigTest_UnknownComponent') + ); + } + + function testGetComponentByType() { + $config = GridFieldConfig::create() + ->addComponent($c1 = new GridFieldConfigTest_MyComponent()) + ->addComponent($c2 = new GridFieldConfigTest_MyOtherComponent()) + ->addComponent($c3 = new GridFieldConfigTest_MyOtherComponent()); + + $this->assertEquals( + $c1, + $config->getComponentByType('GridFieldConfigTest_MyComponent') + ); + $this->assertEquals( + $c2, + $config->getComponentByType('GridFieldConfigTest_MyOtherComponent') + ); + $this->assertNull( + $config->getComponentByType('GridFieldConfigTest_UnknownComponent') + ); + } + +} + +class GridFieldConfigTest_MyComponent implements GridField_URLHandler, TestOnly { + function getURLHandlers($gridField) {return array();} +} + +class GridFieldConfigTest_MyOtherComponent implements GridField_URLHandler, TestOnly { + function getURLHandlers($gridField) {return array();} +} \ No newline at end of file From a135b7c01df8034e414e8dc29cebb98efaf110d1 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Mon, 30 Jan 2012 15:51:07 +0100 Subject: [PATCH 2/2] MINOR Removed GridField->getDefaultConfig() in favour of new GridFieldConfig_Base class (and adjusted its components to remove the "edit" component, and add the "filter" component by default, to match the original getDefaultDefaultConfig()) --- forms/gridfield/GridField.php | 16 +--------------- forms/gridfield/GridFieldConfig.php | 2 +- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/forms/gridfield/GridField.php b/forms/gridfield/GridField.php index c1e334cbe..d21158937 100755 --- a/forms/gridfield/GridField.php +++ b/forms/gridfield/GridField.php @@ -87,7 +87,7 @@ class GridField extends FormField { } if(!$config) { - $this->config = $this->getDefaultConfig(); + $this->config = GridFieldConfig_Base::create(); } else { $this->config = $config; } @@ -142,20 +142,6 @@ class GridField extends FormField { return $this; } - /** - * Get a default configuration for this GridField - * - * @return GridFieldConfig - */ - protected function getDefaultConfig() { - $config = GridFieldConfig::create(); - $config->addComponent(new GridFieldSortableHeader()); - $config->addComponent(new GridFieldFilter()); - $config->addComponent(new GridFieldDefaultColumns()); - $config->addComponent(new GridFieldPaginator()); - return $config; - } - /** * Require the default css styling */ diff --git a/forms/gridfield/GridFieldConfig.php b/forms/gridfield/GridFieldConfig.php index 33b4cd5f9..30ba84151 100755 --- a/forms/gridfield/GridFieldConfig.php +++ b/forms/gridfield/GridFieldConfig.php @@ -85,8 +85,8 @@ class GridFieldConfig_Base extends GridFieldConfig { */ public function __construct($itemsPerPage=25) { $this->addComponent(new GridFieldSortableHeader()); + $this->addComponent(new GridFieldFilter()); $this->addComponent(new GridFieldDefaultColumns()); - $this->addComponent(new GridFieldAction_Edit()); $this->addComponent(new GridFieldPaginator($itemsPerPage)); } }