diff --git a/forms/gridfield/GridFieldConfig.php b/forms/gridfield/GridFieldConfig.php index d305fcd7f..b6a311607 100755 --- a/forms/gridfield/GridFieldConfig.php +++ b/forms/gridfield/GridFieldConfig.php @@ -53,6 +53,27 @@ class GridFieldConfig { return $this; } + /** + * @param GridFieldComponent $component + * @return GridFieldConfig $this + */ + public function removeComponent(GridFieldComponent $component) { + $this->getComponents()->remove($component); + return $this; + } + + /** + * @param String Class name or interface + * @return GridFieldConfig $this + */ + public function removeComponentsByType($type) { + $components = $this->getComponentsByType($type); + foreach($components as $component) { + $this->removeComponent($component); + } + return $this; + } + /** * @return ArrayList Of GridFieldComponent */ diff --git a/tests/forms/gridfield/GridFieldConfigTest.php b/tests/forms/gridfield/GridFieldConfigTest.php index f80447277..e979f5a2b 100644 --- a/tests/forms/gridfield/GridFieldConfigTest.php +++ b/tests/forms/gridfield/GridFieldConfigTest.php @@ -82,6 +82,37 @@ class GridFieldConfigTest extends SapphireTest { ); } + public function testRemoveComponents() { + $config = GridFieldConfig::create() + ->addComponent($c1 = new GridFieldConfigTest_MyComponent()) + ->addComponent($c2 = new GridFieldConfigTest_MyComponent()) + ->addComponent($c3 = new GridFieldConfigTest_MyOtherComponent()) + ->addComponent($c4 = new GridFieldConfigTest_MyOtherComponent()); + + $this->assertEquals( + 4, + $config->getComponents()->count() + ); + + $config->removeComponent($c1); + $this->assertEquals( + 3, + $config->getComponents()->count() + ); + + $config->removeComponentsByType("GridFieldConfigTest_MyComponent"); + $this->assertEquals( + 2, + $config->getComponents()->count() + ); + + $config->removeComponentsByType("GridFieldConfigTest_MyOtherComponent"); + $this->assertEquals( + 0, + $config->getComponents()->count() + ); + } + } class GridFieldConfigTest_MyComponent implements GridField_URLHandler, TestOnly {