mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #6496 from robbieaverill/feature/remove-gridfield-components
API Enhancement: Allow "removeComponentsByType" to remove multiple component"s"
This commit is contained in:
commit
c4972da37e
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user