You are here

function google_map_field_library_info_alter in Google Map Field 8

Implements hook_library_info_alter().

Change the google map library url to add the custom Google API key.

File

./google_map_field.module, line 69
Contains google_map_field.module.

Code

function google_map_field_library_info_alter(&$libraries, $extension) {
  $config = \Drupal::config('google_map_field.settings');
  $settings = $config
    ->get();
  if (isset($libraries['google-map-apis'])) {
    $old_path = array_keys($libraries['google-map-apis']['js']);
    $old_path = $old_path[0];
    if (strpos($old_path, 'key') == FALSE) {
      $js_url = parse_url($old_path);
      parse_str($js_url['query'], $js_url_query);

      // Modify the query parameters.
      unset($js_url_query['sensor']);
      switch ($settings['google_map_field_auth_method']) {
        case 1:
          $js_url_query['key'] = $settings['google_map_field_apikey'];
          break;
        case 2:
          $js_url_query['client'] = $settings['google_map_field_map_client_id'];
          break;
      }

      // Build the new js url with the modified params.
      $js_url['query'] = http_build_query($js_url_query);
      $new_js_url = '//' . $js_url['host'] . $js_url['path'] . '?' . $js_url['query'];
      $new_js = [
        $new_js_url => [],
      ];
      foreach ($libraries['google-map-apis']['js'][$old_path] as $key => $option) {
        $new_js[$new_js_url][$key] = $option;
      }
      $libraries['google-map-apis']['js'] = $new_js;
    }
  }
}