You are here

function webform_location_geocomplete_js_alter in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_location_geocomplete/webform_location_geocomplete.module \webform_location_geocomplete_js_alter()

Implements hook_js_alter().

File

modules/webform_location_geocomplete/webform_location_geocomplete.module, line 34
Provides a form element to collect valid location information using Google Maps API's Geocoding and Places Autocomplete.

Code

function webform_location_geocomplete_js_alter(&$javascript, AttachedAssetsInterface $assets) {

  // Add Google API key required by webform/libraries.jquery.geocomplete
  // which is dependency for webform_location_geocomplete/webform_location_geocomplete.element.
  //
  // @see \Drupal\webform_location_geocomplete\Element\WebformLocationGeocomplete::processWebformComposite
  // @see webform_location_geocomplete.libraries.yml
  $settings = $assets
    ->getSettings();
  if (!empty($settings['webform']['location']['geocomplete']['api_key']) && isset($javascript['https://maps.googleapis.com/maps/api/js?key=API_KEY&libraries=places'])) {
    $api_key = $settings['webform']['location']['geocomplete']['api_key'];
    $javascript['https://maps.googleapis.com/maps/api/js?key=API_KEY&libraries=places']['data'] = "https://maps.googleapis.com/maps/api/js?key={$api_key}&libraries=places";
    unset($settings['webform']['location']['geocomplete']['api_key']);
    $assets
      ->setSettings($settings);
  }
}