You are here

function colorized_gmap_library_info_alter in Colorized google maps block 8

Implements hook_library_info_alter().

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

File

./colorized_gmap.module, line 15
Colorized gmap module file.

Code

function colorized_gmap_library_info_alter(&$libraries, $extension) {
  if (isset($libraries['colorized_gmap.gmap_api'])) {
    $old_path = array_keys($libraries['colorized_gmap.gmap_api']['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']);
      $config = \Drupal::config('colorized_gmap.settings');
      $settings = $config
        ->get();
      $auth_method = isset($settings['colorized_gmap_auth_method']) ? $settings['colorized_gmap_auth_method'] : 1;
      switch ($auth_method) {
        case 1:
          $js_url_query['key'] = isset($settings['colorized_gmap_api_key']) ? $settings['colorized_gmap_api_key'] : '';
          break;
        case 2:
          $js_url_query['client'] = $settings['colorized_gmap_client_id'];
          $js_url_query['signature'] = $settings['colorized_gmap_private_key'];
          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['colorized_gmap.gmap_api']['js'][$old_path] as $key => $option) {
        $new_js[$new_js_url][$key] = $option;
      }
      $libraries['colorized_gmap.gmap_api']['js'] = $new_js;
    }
  }
}