You are here

colorized_gmap.module in Colorized google maps block 8

Same filename and directory in other branches
  1. 7 colorized_gmap.module

Colorized gmap module file.

File

colorized_gmap.module
View source
<?php

/**
 * @file
 * Colorized gmap module file.
 */
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_library_info_alter().
 *
 * Change the google map library url to add the custom Google API key.
 */
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;
    }
  }
}

/**
 * Implements hook_theme().
 */
function colorized_gmap_theme() {
  return [
    'colorized_gmap_output' => [
      'variables' => [
        'machine_name' => NULL,
      ],
      'template' => 'colorized-gmap-output',
    ],
  ];
}

/**
 * Implements hook_form_alter().
 */
function colorized_gmap_form_block_admin_display_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['#attached']['library'][] = 'colorized_gmap/colorized_gmap.gmap_api';
}