Skip to content

Commit e74c44e

Browse files
committed
feature #2350 [Map] Introduce ux_map.google_maps.default_map_id configuration (Kocal)
This PR was merged into the 2.x branch. Discussion ---------- [Map] Introduce `ux_map.google_maps.default_map_id` configuration | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Issues | Fix #2306 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - For new features, provide some code snippets to help understand usage. - Features and deprecations must be submitted against branch main. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> With this modification, I can configure a default Google Maps `mapId`: ```yaml # config/packages/ux_map.yaml ux_map: # https://symfony.com/bundles/ux-map/current/index.html#available-renderers renderer: '%env(resolve:default::UX_MAP_DSN)%' google_maps: default_map_id: abcdefgh123456789 ``` without having to manually pass the `mapId` when creating a `Map`: ```php // use default mapId $map = (new Map()); ->center(new Point(48.8566, 2.3522)) ->zoom(6); // use default mapId $map2 = (clone $map) ->options(new GoogleOptions()); // use custom mapId $map3 = (clone $map) ->options(new GoogleOptions(mapId: 'foobar)); ``` Commits ------- cbacecc [Map] Introduce `ux_map.google_maps.default_map_id` configuration
2 parents 7dff0df + cbacecc commit e74c44e

File tree

9 files changed

+87
-4
lines changed

9 files changed

+87
-4
lines changed

src/Map/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# CHANGELOG
2+
3+
## 2.22
4+
5+
- Add method `Symfony\UX\Map\Renderer\AbstractRenderer::tapOptions()`, to allow Renderer to modify options before rendering a Map.
6+
- Add `ux_map.google_maps.default_map_id` configuration to set the Google ``Map ID``
27

38
## 2.20
49

src/Map/config/twig_component.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1313

1414
use Symfony\UX\Map\Twig\UXMapComponent;
15-
use Symfony\UX\Map\Twig\UXMapComponentListener;
16-
use Symfony\UX\TwigComponent\Event\PreCreateForRenderEvent;
1715

1816
return static function (ContainerConfigurator $container): void {
1917
$container->services()

src/Map/src/Bridge/Google/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## 2.22
4+
5+
- Add support for configuring a default Map ID
6+
- Add argument `$defaultMapId` to `Symfony\UX\Map\Bridge\Google\Renderer\GoogleRendererFactory` constructor
7+
- Add argument `$defaultMapId` to `Symfony\UX\Map\Bridge\Google\Renderer\GoogleRenderer` constructor
8+
39
## 2.20
410

511
### BC Breaks

src/Map/src/Bridge/Google/src/GoogleOptions.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public function mapId(?string $mapId): self
4646
return $this;
4747
}
4848

49+
public function hasMapId(): bool
50+
{
51+
return null !== $this->mapId;
52+
}
53+
4954
public function gestureHandling(GestureHandling $gestureHandling): self
5055
{
5156
$this->gestureHandling = $gestureHandling;

src/Map/src/Bridge/Google/src/Renderer/GoogleRenderer.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function __construct(
4141
* @var array<'core'|'maps'|'places'|'geocoding'|'routes'|'marker'|'geometry'|'elevation'|'streetView'|'journeySharing'|'drawing'|'visualization'>
4242
*/
4343
private array $libraries = [],
44+
private ?string $defaultMapId = null,
4445
) {
4546
parent::__construct($stimulusHelper);
4647
}
@@ -66,7 +67,20 @@ protected function getProviderOptions(): array
6667

6768
protected function getDefaultMapOptions(): MapOptionsInterface
6869
{
69-
return new GoogleOptions();
70+
return new GoogleOptions(mapId: $this->defaultMapId);
71+
}
72+
73+
protected function tapOptions(MapOptionsInterface $options): MapOptionsInterface
74+
{
75+
if (!$options instanceof GoogleOptions) {
76+
throw new \InvalidArgumentException(\sprintf('The options must be an instance of "%s", got "%s" instead.', GoogleOptions::class, get_debug_type($options)));
77+
}
78+
79+
if (!$options->hasMapId()) {
80+
$options->mapId($this->defaultMapId);
81+
}
82+
83+
return $options;
7084
}
7185

7286
public function __toString(): string

src/Map/src/Bridge/Google/src/Renderer/GoogleRendererFactory.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,20 @@
1717
use Symfony\UX\Map\Renderer\Dsn;
1818
use Symfony\UX\Map\Renderer\RendererFactoryInterface;
1919
use Symfony\UX\Map\Renderer\RendererInterface;
20+
use Symfony\UX\StimulusBundle\Helper\StimulusHelper;
2021

2122
/**
2223
* @author Hugo Alliaume <hugo@alliau.me>
2324
*/
2425
final class GoogleRendererFactory extends AbstractRendererFactory implements RendererFactoryInterface
2526
{
27+
public function __construct(
28+
StimulusHelper $stimulus,
29+
private ?string $defaultMapId = null,
30+
) {
31+
parent::__construct($stimulus);
32+
}
33+
2634
public function create(Dsn $dsn): RendererInterface
2735
{
2836
if (!$this->supports($dsn)) {
@@ -42,6 +50,7 @@ public function create(Dsn $dsn): RendererInterface
4250
url: $dsn->getOption('url'),
4351
version: $dsn->getOption('version', 'weekly'),
4452
libraries: ['maps', 'marker', ...$dsn->getOption('libraries', [])],
53+
defaultMapId: $this->defaultMapId,
4554
);
4655
}
4756

src/Map/src/Bridge/Google/tests/GoogleRendererTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,23 @@ public function provideTestRenderMap(): iterable
7878
fullscreenControl: false,
7979
)),
8080
];
81+
82+
yield 'with default map id' => [
83+
'expected_renderer' => '<div data-controller="symfony--ux-google-map--map" data-symfony--ux-google-map--map-provider-options-value="{&quot;apiKey&quot;:&quot;my_api_key&quot;}" data-symfony--ux-google-map--map-view-value="{&quot;center&quot;:{&quot;lat&quot;:48.8566,&quot;lng&quot;:2.3522},&quot;zoom&quot;:12,&quot;fitBoundsToMarkers&quot;:false,&quot;options&quot;:{&quot;mapId&quot;:&quot;DefaultMapId&quot;,&quot;gestureHandling&quot;:&quot;auto&quot;,&quot;backgroundColor&quot;:null,&quot;disableDoubleClickZoom&quot;:false,&quot;zoomControlOptions&quot;:{&quot;position&quot;:22},&quot;mapTypeControlOptions&quot;:{&quot;mapTypeIds&quot;:[],&quot;position&quot;:14,&quot;style&quot;:0},&quot;streetViewControlOptions&quot;:{&quot;position&quot;:22},&quot;fullscreenControlOptions&quot;:{&quot;position&quot;:20}},&quot;markers&quot;:[],&quot;polygons&quot;:[]}"></div>',
84+
'renderer' => new GoogleRenderer(new StimulusHelper(null), 'my_api_key', defaultMapId: 'DefaultMapId'),
85+
'map' => (clone $map),
86+
];
87+
yield 'with default map id, when passing options (except the "mapId")' => [
88+
'expected_renderer' => '<div data-controller="symfony--ux-google-map--map" data-symfony--ux-google-map--map-provider-options-value="{&quot;apiKey&quot;:&quot;my_api_key&quot;}" data-symfony--ux-google-map--map-view-value="{&quot;center&quot;:{&quot;lat&quot;:48.8566,&quot;lng&quot;:2.3522},&quot;zoom&quot;:12,&quot;fitBoundsToMarkers&quot;:false,&quot;options&quot;:{&quot;mapId&quot;:&quot;DefaultMapId&quot;,&quot;gestureHandling&quot;:&quot;auto&quot;,&quot;backgroundColor&quot;:null,&quot;disableDoubleClickZoom&quot;:false,&quot;zoomControlOptions&quot;:{&quot;position&quot;:22},&quot;mapTypeControlOptions&quot;:{&quot;mapTypeIds&quot;:[],&quot;position&quot;:14,&quot;style&quot;:0},&quot;streetViewControlOptions&quot;:{&quot;position&quot;:22},&quot;fullscreenControlOptions&quot;:{&quot;position&quot;:20}},&quot;markers&quot;:[],&quot;polygons&quot;:[]}"></div>',
89+
'renderer' => new GoogleRenderer(new StimulusHelper(null), 'my_api_key', defaultMapId: 'DefaultMapId'),
90+
'map' => (clone $map)
91+
->options(new GoogleOptions()),
92+
];
93+
yield 'with default map id overridden by option "mapId"' => [
94+
'expected_renderer' => '<div data-controller="symfony--ux-google-map--map" data-symfony--ux-google-map--map-provider-options-value="{&quot;apiKey&quot;:&quot;my_api_key&quot;}" data-symfony--ux-google-map--map-view-value="{&quot;center&quot;:{&quot;lat&quot;:48.8566,&quot;lng&quot;:2.3522},&quot;zoom&quot;:12,&quot;fitBoundsToMarkers&quot;:false,&quot;options&quot;:{&quot;mapId&quot;:&quot;CustomMapId&quot;,&quot;gestureHandling&quot;:&quot;auto&quot;,&quot;backgroundColor&quot;:null,&quot;disableDoubleClickZoom&quot;:false,&quot;zoomControlOptions&quot;:{&quot;position&quot;:22},&quot;mapTypeControlOptions&quot;:{&quot;mapTypeIds&quot;:[],&quot;position&quot;:14,&quot;style&quot;:0},&quot;streetViewControlOptions&quot;:{&quot;position&quot;:22},&quot;fullscreenControlOptions&quot;:{&quot;position&quot;:20}},&quot;markers&quot;:[],&quot;polygons&quot;:[]}"></div>',
95+
'renderer' => new GoogleRenderer(new StimulusHelper(null), 'my_api_key', defaultMapId: 'DefaultMapId'),
96+
'map' => (clone $map)
97+
->options(new GoogleOptions(mapId: 'CustomMapId')),
98+
];
8199
}
82100
}

src/Map/src/Renderer/AbstractRenderer.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ abstract protected function getProviderOptions(): array;
3131

3232
abstract protected function getDefaultMapOptions(): MapOptionsInterface;
3333

34+
/**
35+
* @template T of MapOptionsInterface
36+
*
37+
* @param T $options
38+
*
39+
* @return T
40+
*/
41+
protected function tapOptions(MapOptionsInterface $options): MapOptionsInterface
42+
{
43+
return $options;
44+
}
45+
3446
final public function renderMap(Map $map, array $attributes = []): string
3547
{
3648
if (!$map->hasOptions()) {
@@ -39,6 +51,8 @@ final public function renderMap(Map $map, array $attributes = []): string
3951
$map->options($defaultMapOptions);
4052
}
4153

54+
$map->options($this->tapOptions($map->getOptions()));
55+
4256
$controllers = [];
4357
if ($attributes['data-controller'] ?? null) {
4458
$controllers[$attributes['data-controller']] = [];

src/Map/src/UXMapBundle.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ public function configure(DefinitionConfigurator $definition): void
4444
$rootNode
4545
->children()
4646
->scalarNode('renderer')->defaultNull()->end()
47+
->arrayNode('google_maps')
48+
->addDefaultsIfNotSet()
49+
->children()
50+
->scalarNode('default_map_id')->defaultNull()->end()
51+
->end()
52+
->end()
4753
->end()
4854
;
4955
}
@@ -75,9 +81,17 @@ public function loadExtension(array $config, ContainerConfigurator $container, C
7581
foreach (self::$bridges as $name => $bridge) {
7682
if (ContainerBuilder::willBeAvailable('symfony/ux-'.$name.'-map', $bridge['renderer_factory'], ['symfony/ux-map'])) {
7783
$container->services()
78-
->set('ux_map.renderer_factory.'.$name, $bridge['renderer_factory'])
84+
->set($rendererFactoryName = 'ux_map.renderer_factory.'.$name, $bridge['renderer_factory'])
7985
->parent('ux_map.renderer_factory.abstract')
8086
->tag('ux_map.renderer_factory');
87+
88+
if ('google' === $name) {
89+
$container->services()
90+
->get($rendererFactoryName)
91+
->args([
92+
'$defaultMapId' => $config['google_maps']['default_map_id'],
93+
]);
94+
}
8195
}
8296
}
8397
}

0 commit comments

Comments
 (0)