Merge pull request #6496 from robbieaverill/feature/remove-gridfield-components

API Enhancement: Allow "removeComponentsByType" to remove multiple component"s"
This commit is contained in:
Daniel Hensby 2017-01-13 11:29:04 +00:00 committed by GitHub
commit c4972da37e
2 changed files with 32 additions and 5 deletions

View File

@ -91,15 +91,22 @@ class GridFieldConfig extends Object
}
/**
* @param string $type Class name or interface
* @param string|string[] $types Class name or interface, or an array of the same
* @return $this
*/
public function removeComponentsByType($type)
public function removeComponentsByType($types)
{
$components = $this->getComponentsByType($type);
foreach ($components as $component) {
$this->removeComponent($component);
if (!is_array($types)) {
$types = [$types];
}
foreach ($types as $type) {
$components = $this->getComponentsByType($type);
foreach ($components as $component) {
$this->removeComponent($component);
}
}
return $this;
}

View File

@ -123,4 +123,24 @@ class GridFieldConfigTest extends SapphireTest
$config->getComponents()->count()
);
}
/**
* Test that components can be removed with an array of class names or interfaces
*/
public function testRemoveMultipleComponents()
{
$config = GridFieldConfig::create()
->addComponent(new MyComponent)
->addComponent(new MyComponent)
->addComponent(new MyOtherComponent);
$config->removeComponentsByType(
[
MyComponent::class,
MyOtherComponent::class
]
);
$this->assertSame(0, $config->getComponents()->count());
}
}