diff --git a/config/install/geocoder.settings.yml b/config/install/geocoder.settings.yml index d7211bb..b21abc0 100644 --- a/config/install/geocoder.settings.yml +++ b/config/install/geocoder.settings.yml @@ -1 +1,2 @@ -cache: true \ No newline at end of file +cache: true +api_key: '' \ No newline at end of file diff --git a/geocoder.links.menu.yml b/geocoder.links.menu.yml index 6ec2db5..0a5f9a1 100644 --- a/geocoder.links.menu.yml +++ b/geocoder.links.menu.yml @@ -1,6 +1,6 @@ geocoder.settings: title: 'Geocoder configuration' description: 'Configure the Geocoder module.' + parent: system.admin_config_system route_name: geocoder.settings weight: 10 - parent: system.admin_config_system diff --git a/geocoder.links.task.yml b/geocoder.links.task.yml new file mode 100644 index 0000000..653628d --- /dev/null +++ b/geocoder.links.task.yml @@ -0,0 +1,10 @@ +geocoder.settings: + route_name: geocoder.settings + title: 'Settings' + base_route: geocoder.settings + weight: -1 + +geocoder.settings_api_keys: + route_name: geocoder.settings_api_keys + title: 'API keys' + base_route: geocoder.settings \ No newline at end of file diff --git a/geocoder.routing.yml b/geocoder.routing.yml index c5f9489..9b5c12f 100644 --- a/geocoder.routing.yml +++ b/geocoder.routing.yml @@ -5,3 +5,11 @@ geocoder.settings: _title: 'Geocoder configuration' requirements: _permission: 'administer site configuration' + +geocoder.settings_api_keys: + path: '/admin/config/system/geocoder/api-keys' + defaults: + _form: '\Drupal\geocoder\Form\APIKeysForm' + _title: 'API Keys' + requirements: + _permission: 'access administration pages' \ No newline at end of file diff --git a/src/Form/APIKeysForm.php b/src/Form/APIKeysForm.php new file mode 100644 index 0000000..ac15184 --- /dev/null +++ b/src/Form/APIKeysForm.php @@ -0,0 +1,60 @@ +config('geocoder.google'); + + $form['google_api_key'] = [ + '#type' => 'textfield', + '#title' => $this->t("Google API Key"), + '#default_value' => $googleConfig->get('api_key') + ]; + + $form['actions']['#type'] = 'actions'; + + $form['actions']['submit'] = [ + '#type' => 'submit', + '#value' => $this->t('Save'), + '#button_type' => 'primary', + ]; + + return $form; + } + + public function submitForm(array &$form, FormStateInterface $form_state) { + $this->config('geocoder.google') + ->set('api_key', $form_state->getValue('google_api_key')) + ->save(); + + parent::submitForm($form, $form_state); + } +} \ No newline at end of file diff --git a/src/Plugin/Geocoder/Provider/GoogleMaps.php b/src/Plugin/Geocoder/Provider/GoogleMaps.php index 20abc1f..f40aeda 100644 --- a/src/Plugin/Geocoder/Provider/GoogleMaps.php +++ b/src/Plugin/Geocoder/Provider/GoogleMaps.php @@ -19,4 +19,22 @@ use Drupal\geocoder\ProviderUsingHandlerWithAdapterBase; * } * ) */ -class GoogleMaps extends ProviderUsingHandlerWithAdapterBase {} +class GoogleMaps extends ProviderUsingHandlerWithAdapterBase { + + /** + * {@inheritdoc} + */ + protected function getArguments() { + $arguments = parent::getArguments(); + $api_key = \Drupal::config('geocoder.google')->get('api_key'); + + // Add the API key to the provider arguments. + if (!empty($api_key)) { + // For proper API calls, we have to go through SSL. + $arguments[3] = true; + $arguments[4] = $api_key; + } + + return $arguments; + } +}