You are here

function styled_google_views_preprocess_styled_google_view in Styled Google Map 7.2

Same name and namespace in other branches
  1. 7 styled_google_views/styled_google_views.module \styled_google_views_preprocess_styled_google_view()

Implements hook_preprocess_HOOK().

  • Preprocess the styled google map view template.

File

styled_google_views/styled_google_views.module, line 36
Styled Google Views module.

Code

function styled_google_views_preprocess_styled_google_view(&$vars) {

  // If pathauto module exists include the pathauto.inc file to get
  // pathauto_cleanstring function to have safe strings for pin category.
  if (module_exists('pathauto')) {
    module_load_include('inc', 'pathauto');
  }

  // Do not render anything in the live preview for avoiding errors.
  if (isset($vars['view']->live_preview) && $vars['view']->live_preview) {
    return;
  }
  drupal_add_js(drupal_get_path('module', 'styled_google_map') . '/styled-google-map.js');

  // Build API url.
  $api_url = styled_google_map_build_api_url();

  // Include the Google Maps API.
  drupal_add_js($api_url, array(
    'type' => 'external',
    'group' => JS_LIBRARY,
  ));
  drupal_add_js(drupal_get_path('module', 'styled_google_map') . '/lib/markerclusterer.js');
  drupal_add_js(drupal_get_path('module', 'styled_google_map') . '/lib/infobubble.js');

  // Get all geofield locations.
  $locations = array();
  foreach ($vars['view']->result as $row_index => $row) {
    $location = array();
    if (isset($vars['options']['data_source']) && !empty($vars['options']['data_source'])) {

      // Add geofield data.
      if (isset($row->{'field_' . $vars['options']['data_source']}[0])) {
        $location = $location + $row->{'field_' . $vars['options']['data_source']}[0]['raw'];
      }
      elseif (isset($row->_field_data[$vars['options']['data_source']]['entity']->{$vars['options']['data_source']}[LANGUAGE_NONE][0])) {
        $location = $location + $row->_field_data[$vars['options']['data_source']]['entity']->{$vars['options']['data_source']}[LANGUAGE_NONE][0];
      }
      else {
        continue;
      }

      // Add pin image url.
      if (isset($vars['options']['pin_source']) && !empty($vars['options']['pin_source'])) {
        if (isset($row->{'field_' . $vars['options']['pin_source']}[0])) {
          $location = $location + array(
            'pin' => file_create_url($row->{'field_' . $vars['options']['pin_source']}[0]['raw']['uri']),
          );

          // Add the active pin image.
          if (!$vars['options']['styled_google_map_view_active_pin']) {
            $location = $location + array(
              'active_pin' => file_create_url($row->{'field_' . $vars['options']['pin_source']}[0]['raw']['uri']),
            );
          }
          else {
            $location = $location + array(
              'active_pin' => file_create_url($vars['options']['styled_google_map_view_active_pin']),
            );
          }
        }
      }
      elseif (isset($row->_field_data[$vars['options']['pin_source']]['entity']->{$vars['options']['pin_source']}[LANGUAGE_NONE][0])) {
        $location = $location + array(
          'pin' => file_create_url($row->_field_data[$vars['options']['pin_source']]['entity']->{$vars['options']['pin_source']}[LANGUAGE_NONE][0]['uri']),
        );

        // Add the active pin image.
        if (!$vars['options']['styled_google_map_view_active_pin']) {
          $location = $location + array(
            'active_pin' => file_create_url($row->_field_data[$vars['options']['pin_source']]['entity']->{$vars['options']['pin_source']}[LANGUAGE_NONE][0]['uri']),
          );
        }
        else {
          $location = $location + array(
            'active_pin' => file_create_url($vars['options']['styled_google_map_view_active_pin']),
          );
        }
      }
      if ($vars['options']['styled_google_map_view_default_pin'] && empty($location['pin'])) {
        $location['pin'] = file_create_url($vars['options']['styled_google_map_view_default_pin']);
      }

      // Add pin popup html.
      if (isset($vars['options']['popup_source']) && !empty($vars['options']['popup_source'])) {

        // Necessary to avoid warnings, see https://www.drupal.org/node/2757971.
        $vars['view']->row_index = 0;
        $location = $location + array(
          'popup' => $vars['view']->style_plugin->rendered_fields[$row_index][$vars['options']['popup_source']],
        );
      }

      // Add category.
      if (isset($vars['options']['category_source']) && !empty($vars['options']['category_source'])) {
        if (module_exists('pathauto')) {
          $category = pathauto_cleanstring($vars['view']->field[$vars['options']['category_source']]
            ->theme($row));
        }
        else {
          $category = $vars['view']->field[$vars['options']['category_source']]
            ->theme($row);
        }
        $location = $location + array(
          'category' => $category,
        );
      }
    }
    if ($location) {
      $locations[] = $location;
    }
  }

  // Add custom settings.
  $map_settings = array();
  $map_settings['styled_google_map'][] = array(
    'styled-google-map-' . $vars['view']->vid,
  );
  $cluster_pin_image = '';
  $active_pin_image = '';
  if ($vars['options']['styled_google_map_view_cluster_pin']) {
    $cluster_pin_image = file_create_url($vars['options']['styled_google_map_view_cluster_pin']);
  }

  // TODO: sanitize all options.
  $map_settings['styled-google-map-' . $vars['view']->vid] = array(
    'id' => 'styled-google-map-' . $vars['view']->vid,
    'locations' => $locations,
    'settings' => array(
      'height' => $vars['options']['styled_google_map_view_height'],
      'width' => $vars['options']['styled_google_map_view_width'],
      'maptypecontrol' => $vars['options']['styled_google_map_view_maptypecontrol'],
      'scalecontrol' => $vars['options']['styled_google_map_view_scalecontrol'],
      'rotatecontrol' => $vars['options']['styled_google_map_view_rotatecontrol'],
      'draggable' => $vars['options']['styled_google_map_view_draggable'],
      'mobile_draggable' => $vars['options']['styled_google_map_view_mobile_draggable'],
      'scrollwheel' => $vars['options']['styled_google_map_view_scrollwheel'],
      'streetviewcontrol' => $vars['options']['styled_google_map_view_streetviewcontrol'],
      'style' => array(
        'maptype' => $vars['options']['styled_google_map_view_maptype'],
        'style' => $vars['options']['styled_google_map_view_style'],
        'active_pin' => $active_pin_image,
        'cluster_pin' => $cluster_pin_image,
      ),
      'map_center' => array(
        'center_coordinates' => array(
          'lat' => $vars['options']['map_center']['styled_google_map_view_center_coordinates_lat'],
          'lon' => $vars['options']['map_center']['styled_google_map_view_center_coordinates_lon'],
        ),
      ),
      'zoom' => array(
        'default' => $vars['options']['styled_google_map_view_zoom_default'],
        'max' => $vars['options']['styled_google_map_view_zoom_max'],
        'min' => $vars['options']['styled_google_map_view_zoom_min'],
      ),
      'zoomcontrol' => $vars['options']['styled_google_map_view_zoomcontrol'],
      'popup' => array(
        'disable_animation' => $vars['options']['popup']['styled_google_map_view_disable_animation'] ? TRUE : FALSE,
        'disable_autopan' => $vars['options']['popup']['styled_google_map_view_disable_auto_pan'] ? TRUE : FALSE,
        'second_click' => $vars['options']['popup']['styled_google_map_view_second_click'],
        'hide_close_button' => $vars['options']['popup']['styled_google_map_view_hide_close_button'] ? TRUE : FALSE,
        'shadow_style' => $vars['options']['popup']['styled_google_map_view_shadow_style'],
        'padding' => $vars['options']['popup']['styled_google_map_view_padding'],
        'border_radius' => $vars['options']['popup']['styled_google_map_view_border_radius'],
        'border_width' => $vars['options']['popup']['styled_google_map_view_border_width'],
        'border_color' => $vars['options']['popup']['styled_google_map_view_border_color'],
        'background_color' => $vars['options']['popup']['styled_google_map_view_background_color'],
        'min_width' => $vars['options']['popup']['styled_google_map_view_min_width'],
        'max_width' => $vars['options']['popup']['styled_google_map_view_max_width'],
        'min_height' => $vars['options']['popup']['styled_google_map_view_min_height'],
        'max_height' => $vars['options']['popup']['styled_google_map_view_max_height'],
        'arrow_style' => $vars['options']['popup']['styled_google_map_view_arrow_style'],
        'arrow_size' => $vars['options']['popup']['styled_google_map_view_arrow_size'],
        'arrow_position' => $vars['options']['popup']['styled_google_map_view_arrow_position'],
        'classes' => array(
          'container' => $vars['options']['popup_classes']['styled_google_map_view_content_container_class'],
          'background' => $vars['options']['popup_classes']['styled_google_map_view_background_class'],
          'arrow' => $vars['options']['popup_classes']['styled_google_map_view_arrow_class'],
          'arrow_outer' => $vars['options']['popup_classes']['styled_google_map_view_arrow_outer_class'],
          'arrow_inner' => $vars['options']['popup_classes']['styled_google_map_view_arrow_inner_class'],
        ),
      ),
    ),
  );
  drupal_add_js($map_settings, 'setting');
}