[Map] Add Map::removeAll*() methods

This commit is contained in:
Steven Renaux
2025-12-21 00:00:47 +01:00
committed by Hugo Alliaume
parent bb30afa5f3
commit 97e5fd66c6
4 changed files with 62 additions and 0 deletions

View File

@@ -1,5 +1,9 @@
# CHANGELOG # CHANGELOG
## 2.32
- Add `Map::removeAllMarkers()`, `Map::removeAllPolygons()`, `Map::removeAllPolylines()`, `Map::removeAllCircles()` and `Map::removeAllRectangles()` methods
## 2.31 ## 2.31
- Add `fitBoundsToMarkers` parameter to `ux_map()` Twig function - Add `fitBoundsToMarkers` parameter to `ux_map()` Twig function

View File

@@ -292,6 +292,22 @@ If you haven't stored the element instance, you can still remove them by passing
$map->removeCircle('my-circle'); $map->removeCircle('my-circle');
$map->removeRectangle('my-rectangle'); $map->removeRectangle('my-rectangle');
To remove all instances of a certain element, you can use the `Map::removeAll*()` methods::
// Add elements
$map->addMarker($marker = new Marker(/* ... */));
$map->addPolygon($polygon = new Polygon(/* ... */));
$map->addPolyline($polyline = new Polyline(/* ... */));
$map->addCircle($circle = new Circle(/* ... */));
$map->addRectangle($rectangle = new Rectangle(/* ... */));
// And later, remove those elements
$map->removeAllMarkers();
$map->removeAllPolygons();
$map->removeAllPolylines();
$map->removeAllCircles();
$map->removeAllRectangles();
Render a map Render a map
------------ ------------

View File

@@ -64,6 +64,13 @@ abstract class Elements
return $this; return $this;
} }
public function removeAll(): static
{
$this->elements->removeAll($this->elements);
return $this;
}
public function toArray(): array public function toArray(): array
{ {
foreach ($this->elements as $element) { foreach ($this->elements as $element) {

View File

@@ -133,6 +133,13 @@ final class Map
return $this; return $this;
} }
public function removeAllMarkers(): self
{
$this->markers->removeAll();
return $this;
}
public function addPolygon(Polygon $polygon): self public function addPolygon(Polygon $polygon): self
{ {
$this->polygons->add($polygon); $this->polygons->add($polygon);
@@ -147,6 +154,13 @@ final class Map
return $this; return $this;
} }
public function removeAllPolygons(): self
{
$this->polygons->removeAll();
return $this;
}
public function addPolyline(Polyline $polyline): self public function addPolyline(Polyline $polyline): self
{ {
$this->polylines->add($polyline); $this->polylines->add($polyline);
@@ -161,6 +175,13 @@ final class Map
return $this; return $this;
} }
public function removeAllPolylines(): self
{
$this->polylines->removeAll();
return $this;
}
public function addCircle(Circle $circle): self public function addCircle(Circle $circle): self
{ {
$this->circles->add($circle); $this->circles->add($circle);
@@ -175,6 +196,13 @@ final class Map
return $this; return $this;
} }
public function removeAllCircles(): self
{
$this->circles->removeAll();
return $this;
}
public function addRectangle(Rectangle $rectangle): self public function addRectangle(Rectangle $rectangle): self
{ {
$this->rectangles->add($rectangle); $this->rectangles->add($rectangle);
@@ -189,6 +217,13 @@ final class Map
return $this; return $this;
} }
public function removeAllRectangles(): self
{
$this->rectangles->removeAll();
return $this;
}
/** /**
* @param array<string, mixed> $extra Extra data forwarded to the JavaScript side. It can be used in your custom * @param array<string, mixed> $extra Extra data forwarded to the JavaScript side. It can be used in your custom
* Stimulus controller to benefit from greater flexibility and customization. * Stimulus controller to benefit from greater flexibility and customization.