Skip to content

Commit 667b044

Browse files
authored
improve google maps key fetch and docs (#133)
1 parent 87a1e00 commit 667b044

File tree

4 files changed

+78
-19
lines changed

4 files changed

+78
-19
lines changed

UPGRADE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Just click the "update" button or execute the migration command to finish the bu
99
#### Update from Version 3.1.x to Version 3.2.0
1010
- **[NEW FEATURE]**: Pimcore 6.4.0 and Pimcore 6.5.0 ready
1111
- **[NEW FEATURE]**: Store Provider added (https://github.com/dachcom-digital/pimcore-toolbox/pull/128)
12+
- **[IMPROVEMENT]**: Better Google API Key Fetching: Introduces `toolbox.google_maps.browser_api_key` and `toolbox.google_maps.simple_api_key`. Read more about it [here](./docs/11_ElementsOverview.md#google-map)
1213
- **[BUG FIX]**: Video Autoplay Fix (https://github.com/dachcom-digital/pimcore-toolbox/issues/129)
1314

1415
#### Update from Version 3.x to Version 3.1.0

docs/11_ElementsOverview.md

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,26 +179,54 @@ If you want to use a different extension to generate your galleries, just overri
179179

180180
## Google Map
181181
Create a Google Map Element. You're able to define one or multiple markers. Toolbox will automatically generate the long/lat information after saving the document.
182-
Please make sure that you've included a valid google maps api key. Include the script tag in your footer:
183182

184-
```html
185-
<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?libraries=places&amp;key={{ toolbox_google_map_api_key() }}"></script>
186-
```
183+
### Get & set your API Key
184+
To get a valid API Key you need to follow those steps:
185+
- Create [a valid google maps api key](https://developers.google.com/maps/documentation/javascript/get-api-key)
186+
- Enable two APIs: Geocoding API, Maps Javascript API
187+
- Generate an API Key
188+
- Copy API Key
189+
190+
### Key Integration
191+
You have three ways to integrate your Keys.
187192

188-
Now head into the pimcore-backend, open systemsettings, navigate to Google Credentials & API Keys and insert the api key for google maps into the browser-api-key-field:
193+
#### I. Pimcore Configuration
194+
195+
> Note: This is the recommended way to integrate GM Keys!
196+
197+
Head into the pimcore backend, open system settings, navigate to Google Credentials & API Keys and insert the api key for google maps into the browser-api-key-field:
189198

190199
![2_7_1_google_api_keys.png](./img/2_7_1_google_api_keys.png)
191200

192-
If you made your own toolbox-/googleMap-config, please make sure to add the last line to it:
201+
#### II. Using a Parameter
202+
If you want to define them via parameters, you're able to define them like this:
203+
204+
```yaml
205+
# app/config/parameters.yml
206+
parameters:
207+
toolbox_google_service_browser_api_key: YOUR_BROWSER_KEY
208+
toolbox_google_service_simple_api_key: YOUR_SIMPLE_KEY
209+
```
210+
211+
#### III. Direct Element Configuration
212+
If you don't want do define them via parameters, you need to override the default parameters
213+
on configuration layer:
214+
193215
```yaml
194216
toolbox:
195217
areas:
196218
googleMap:
197219
config_parameter:
198-
map_api_key: '%pimcore_system_config.services.google.browserapikey%'
220+
map_api_key: '%toolbox.google_maps.browser_api_key%' # replace here
221+
simple_api_key: '%toolbox.google_maps.simple_api_key%' # replace here
199222
```
200223

201-
> Note: like all other systemsettings, this one is also stored in system.php for versioning
224+
### Script Integration
225+
Now you can include the script tag in your footer:
226+
227+
```html
228+
<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?libraries=places&amp;key={{ toolbox_google_map_api_key() }}"></script>
229+
```
202230

203231
> Note: This is a [custom toolbox element](22_GoogleMapsElement.md).
204232

src/ToolboxBundle/DependencyInjection/ToolboxExtension.php

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,8 @@ class ToolboxExtension extends Extension implements PrependExtensionInterface
2727
*/
2828
public function prepend(ContainerBuilder $container)
2929
{
30-
// prevent throwing exception if no gm key has been defined.
31-
if ($container->hasParameter('pimcore_system_config.services.google.browserapikey') === false) {
32-
$container->setParameter('pimcore_system_config.services.google.browserapikey', null);
33-
}
34-
if ($container->hasParameter('pimcore_system_config.services.google.simpleapikey') === false) {
35-
$container->setParameter('pimcore_system_config.services.google.simpleapikey', null);
36-
}
30+
$container->setParameter('toolbox.google_maps.browser_api_key', null);
31+
$container->setParameter('toolbox.google_maps.simple_api_key', null);
3732

3833
$selfConfigs = $container->getExtensionConfig($this->getAlias());
3934

@@ -118,6 +113,8 @@ public function load(array $configs, ContainerBuilder $container)
118113

119114
$this->validateToolboxContextConfig($config);
120115

116+
$this->allocateGoogleMapsApiKey($container);
117+
121118
$contextResolver = $config['context_resolver'];
122119
unset($config['context_resolver']);
123120

@@ -136,6 +133,39 @@ public function load(array $configs, ContainerBuilder $container)
136133
$definition->setClass($contextResolver);
137134
}
138135

136+
/**
137+
* @param ContainerBuilder $container
138+
*/
139+
private function allocateGoogleMapsApiKey(ContainerBuilder $container)
140+
{
141+
$googleBrowserApiKey = null;
142+
$googleSimpleApiKey = null;
143+
144+
$pimcoreCoreConfig = $container->hasParameter('pimcore.config') ? $container->getParameter('pimcore.config') : [];
145+
$pimcoreGoogleServiceConfig = isset($pimcoreCoreConfig['services']) && isset($pimcoreCoreConfig['services']['google']) ? $pimcoreCoreConfig['services']['google'] : [];
146+
147+
// browser api key
148+
if ($container->hasParameter('pimcore_system_config.services.google.browserapikey') === true) {
149+
$googleBrowserApiKey = $container->getParameter('pimcore_system_config.services.google.browserapikey');
150+
} elseif ($container->hasParameter('toolbox_google_service_browser_api_key') === true) {
151+
$googleBrowserApiKey = $container->getParameter('toolbox_google_service_browser_api_key');
152+
} elseif (isset($pimcoreGoogleServiceConfig['browser_api_key'])) {
153+
$googleBrowserApiKey = $pimcoreGoogleServiceConfig['browser_api_key'];
154+
}
155+
156+
//simple api key
157+
if ($container->hasParameter('pimcore_system_config.services.google.simpleapikey') === true) {
158+
$googleSimpleApiKey = $container->getParameter('pimcore_system_config.services.google.simpleapikey');
159+
} elseif ($container->hasParameter('toolbox_google_service_simple_api_key') === true) {
160+
$googleSimpleApiKey = $container->getParameter('toolbox_google_service_simple_api_key');
161+
} elseif (isset($pimcoreGoogleServiceConfig['simple_api_key'])) {
162+
$googleSimpleApiKey = $pimcoreGoogleServiceConfig['simple_api_key'];
163+
}
164+
165+
$container->setParameter('toolbox.google_maps.browser_api_key', $googleBrowserApiKey);
166+
$container->setParameter('toolbox.google_maps.simple_api_key', $googleSimpleApiKey);
167+
}
168+
139169
/**
140170
* @param array $config
141171
*/
@@ -155,12 +185,12 @@ private function validateToolboxContextConfig($config)
155185
}
156186

157187
/**
158-
* @deprecated since 2.3. gets removed in 4.0
159-
*
160188
* @param array $config
161189
* @param ContainerBuilder $container
162190
*
163191
* @return mixed
192+
* @deprecated since 2.3. gets removed in 4.0
193+
*
164194
*/
165195
private function handleCalculatorDeprecation($config, ContainerBuilder $container)
166196
{

src/ToolboxBundle/Resources/config/pimcore/areas/googleMap.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,5 @@ toolbox:
5050
scrollwheel: false
5151
map_style_url: false
5252
marker_icon: false
53-
map_api_key: '%pimcore_system_config.services.google.browserapikey%'
54-
simple_api_key: '%pimcore_system_config.services.google.simpleapikey%'
53+
map_api_key: '%toolbox.google_maps.browser_api_key%'
54+
simple_api_key: '%toolbox.google_maps.simple_api_key%'

0 commit comments

Comments
 (0)