You are here

function theme_styled_google_map in Styled Google Map 7

Same name and namespace in other branches
  1. 8 styled_google_map.module \theme_styled_google_map()
  2. 7.2 styled_google_map.module \theme_styled_google_map()

Returns HTML for the styled google map.

Parameters

array $variables: An associative array containing:

  • location: The location object including longitude and latitude.
  • display: Display array including formatter settings.
1 theme call to theme_styled_google_map()
styled_google_map_field_formatter_view in ./styled_google_map.module
Implements hook_field_formatter_view().

File

./styled_google_map.module, line 541
Contains all hooks and functions for the Styled Google Map module.

Code

function theme_styled_google_map(array $variables) {
  global $language;
  $location = $variables['location'];
  $display = $variables['display'];
  $entity = $variables['entity'];
  $entity_type = $variables['entity_type'];
  $settings = $display['settings'];
  $output = array();

  // TODO: Split this function so logic is out of the theming function.
  if (!empty($location) && !empty($location['geohash'])) {

    // Get the pin file url.
    if (isset($settings['style']['pin']) && !empty($settings['style']['pin'])) {
      $settings['style']['pin'] = file_create_url($settings['style']['pin']);
    }

    // Sanitize the output of the style settings.
    foreach ($settings['style'] as $id => $setting) {
      $location[$id] = filter_xss($settings['style'][$id]);
    }

    // Get the label settings.
    if (isset($entity) && !empty($entity)) {
      switch ($settings['popup']['choice']) {

        // Create popup from label.
        case 1:
          $settings['popup']['label'] = $settings['popup']['label'] ? 'inline' : 'hidden';
          $popup_field = field_view_field($entity_type, $entity, $settings['popup']['text'], $display = array(
            'label' => $settings['popup']['label'],
          ), $language->language);
          break;

        // Create popup from view mode.
        case 2:
          $popup_field = field_attach_view($entity_type, $entity, $settings['popup']['view_mode'], $language->language);

          // Render field groups in case it is included in the attached view.
          if (module_exists('field_group')) {

            // This is a little hack as we have to pass something to being able
            // to execute the field groups nesting function.
            $entity_reference[] = $entity;
            field_group_fields_nest($popup_field, $entity_reference);
          }
          break;

        // Default to empty.
        default:
          $popup_field = array();
      }
      $location['popup'] = render($popup_field);
      if ($settings['map_center']['center_coordinates']) {
        $map_center = field_get_items($entity_type, $entity, $settings['map_center']['center_coordinates']);
        if ($map_center && isset($map_center[0]['lat']) && isset($map_center[0]['lon'])) {
          $settings['map_center']['center_coordinates'] = $map_center[0];
        }
        else {
          $settings['map_center']['center_coordinates'] = FALSE;
        }
      }
    }
    else {

      // Not an entity object.
      $location['popup'] = array();
    }
    $gid = uniqid();
    drupal_add_js(array(
      'styled_google_map' => array(
        $gid => $gid,
      ),
    ), 'setting');

    // Include the Google Maps API.
    drupal_add_js('//maps.google.com/maps/api/js?sensor=true', array(
      'type' => 'external',
      'group' => JS_LIBRARY,
    ));

    // Include the Info Bubble API.
    drupal_add_js(drupal_get_path('module', 'styled_google_map') . '/lib/infobubble.js');

    // Include the map location settings.
    $map_settings['locations'] = array(
      $location,
    );

    // Include the custom map settings.
    $map_settings['settings'] = $settings;

    // Include the unique div id.
    $map_settings['id'] = 'styled-google-map-' . $gid;
    drupal_add_js(array(
      'id' . $gid => $map_settings,
    ), 'setting');

    // Output a div placeholder for the Styled Google Map.
    $output['styled_google_map']['#markup'] = '<div style="width:' . check_plain($settings['width']) . ';height:' . check_plain($settings['height']) . ';" id="styled-google-map-' . $gid . '"></div>';

    // Attach the Styled Google Map javascript file.
    $output['#attached']['js'][] = drupal_get_path('module', 'styled_google_map') . '/styled-google-map.js';

    // Attach CSS fix, this will prevent the map controls from breaking.
    $output['#attached']['css'][] = array(
      'data' => ".gm-style img {max-width: none;}",
      'type' => 'inline',
    );
  }
  return render($output);
}