You are here

google_map_field.module in Google Map Field 7.2

Same filename and directory in other branches
  1. 8 google_map_field.module
  2. 7 google_map_field.module

This file defines all the necessary hooks and functions to create a Google Map Field field type for inserting maps directly into content items (node, entities etc).

File

google_map_field.module
View source
<?php

/**
 * @file
 * This file defines all the necessary hooks and functions to create
 * a Google Map Field field type for inserting maps directly into
 * content items (node, entities etc).
 */

/**
 * Implements hook_help().
 */
function google_map_field_help($path, $arg) {
  $output = '';
  switch ($path) {
    case 'admin/help#google_map_field':
      $output = t('Add Google Map type fields to content types.');
  }
  return $output;
}

/**
 * Implements hook_permission().
 */
function google_map_field_permission() {
  return array(
    'access google map config' => array(
      'title' => t('Access Google Map API Key'),
      'description' => t('Allow access to define/edit the Google Map API key for the Google Map Field module.'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function google_map_field_menu() {
  $items = array();
  $items['admin/config/development/google-map-field'] = array(
    'title' => 'Google Map Field',
    'description' => 'Google Maps Field config page.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'google_map_field_settings_form',
    ),
    'file' => 'includes/google_map_field.admin.inc',
    'access arguments' => array(
      'access google map config',
    ),
  );
  return $items;
}

/**
 * Implements hook_field_info().
 */
function google_map_field_field_info() {
  return array(
    'google_map_field' => array(
      'label' => t('Google Map Field'),
      'description' => t('This field stores and renders Google Maps.'),
      'default_widget' => 'google_map_field_widget',
      'default_formatter' => 'google_map_field_formatter_default',
    ),
  );
}

/**
 * Implements hook_field_widget_info().
 */
function google_map_field_field_widget_info() {
  return array(
    'google_map_field_widget' => array(
      'label' => t('Map'),
      'field types' => array(
        'google_map_field',
      ),
    ),
  );
}

/**
 * Implements hook_field_formatter_info().
 */
function google_map_field_field_formatter_info() {
  $formats = array(
    'google_map_field_formatter_default' => array(
      'label' => t('Map'),
      'description' => t('Default display for Google map field.'),
      'field types' => array(
        'google_map_field',
      ),
    ),
    'google_map_field_formatter_wkt' => array(
      'label' => t('WKT'),
      'description' => t('Use WKT format to display the google maps point info'),
      'field types' => array(
        'google_map_field',
      ),
    ),
    'google_map_field_formatter_name' => array(
      'label' => t('Map Name'),
      'description' => t('Use Map Name format to display just the name of each map'),
      'field types' => array(
        'google_map_field',
      ),
    ),
    'google_map_field_formatter_longitude' => array(
      'label' => t('Longitude'),
      'description' => t('Use longitude format to display the google maps longitude info'),
      'field types' => array(
        'google_map_field',
      ),
    ),
    'google_map_field_formatter_latitude' => array(
      'label' => t('Latitude'),
      'description' => t('Use latitude format to display the google maps latitude info'),
      'field types' => array(
        'google_map_field',
      ),
    ),
    'google_map_field_formatter_zoom' => array(
      'label' => t('Zoom'),
      'description' => t('Use latitude format to display the google maps latitude info'),
      'field types' => array(
        'google_map_field',
      ),
    ),
  );
  return $formats;
}

/**
 * Implements hook_field_widget_error().
 */
function google_map_field_field_widget_error($element, $error, $form, &$form_state) {
  form_error($element, $error['message']);
}

/**
 * Implements hook_field_is_empty().
 */
function google_map_field_field_is_empty($item, $field) {
  if (empty($item['lat']) || empty($item['lon'])) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Implements hook_field_formatter_view().
 */
function google_map_field_field_formatter_view($object_type, $object, $field, $instance, $langcode, $items, $display) {
  drupal_add_css(drupal_get_path('module', 'google_map_field') . '/css/google_map_field.css');
  $element = array();
  switch ($display['type']) {
    case 'google_map_field_formatter_wkt':
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#markup' => 'POINT(' . $item['lon'] . ' ' . $item['lat'] . ')',
        );
      }
      break;
    case 'google_map_field_formatter_default':
      foreach ($items as $delta => $item) {
        $element[$delta] = google_map_field_format_field($item, $delta, $display);
      }
      break;
    case 'google_map_field_formatter_name':
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#markup' => $item['name'],
        );
      }
      break;
    case 'google_map_field_formatter_longitude':
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#markup' => $item['lon'],
        );
      }
      break;
    case 'google_map_field_formatter_latitude':
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#markup' => $item['lat'],
        );
      }
      break;
    case 'google_map_field_formatter_zoom':
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#markup' => $item['zoom'],
        );
      }
      break;
  }
  return $element;
}

/**
 * This function formats the google map field for display,
 * called by google_map_field_field_formatter_view().
 */
function google_map_field_format_field($item, $delta, $display) {
  require module_load_include('inc', 'google_map_field');
  $js_settings = array();
  foreach ($settings as $type_name => $data) {
    $js_settings[$type_name] = variable_get('google_map_field_' . $type_name, $data['default']);
  }
  drupal_add_js(array(
    'googleMapField' => $js_settings,
  ), 'setting');
  drupal_add_js(drupal_get_path('module', 'google_map_field') . '/js/google_map_field_node_display.js', 'file');
  google_map_field_add_maps_api();
  $element = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'field-item',
      ),
    ),
  );
  $element['google_map_field'] = array(
    'item' => array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'field-item',
        ),
      ),
      'text' => array(
        '#markup' => theme('google_map_field', array(
          'name' => check_plain($item['name']),
          'lat' => $item['lat'],
          'lng' => $item['lon'],
          'zoom' => $item['zoom'],
        )),
      ),
    ),
  );
  return $element;
}

/**
 * Implements hook_field_widget_form().
 */
function google_map_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $form['#attached']['js'][] = google_map_field_get_protocol() . 'maps.googleapis.com/maps/api/js?sensor=false&key=' . variable_get('google_map_field_apikey', '');
  $field_id = is_a($element['#entity'], 'FieldCollectionItemEntity') ? $element['#entity']->item_id : $field['id'];
  $field_id = empty($field_id) ? rand(10000000, 99999999) : $field_id;
  $element['#title'] = t('Define Location');
  $element['#description'] = '<p>' . t('To set a location for the map, click the "Set Map Marker" button, or enter the latitude/longitude in the field provided.') . '</p>';
  $element += array(
    '#type' => 'fieldset',
    '#collapsed' => FALSE,
    '#collapsible' => TRUE,
    '#attached' => array(
      'library' => array(
        array(
          'system',
          'ui.dialog',
        ),
      ),
      'js' => array(
        drupal_get_path('module', 'google_map_field') . '/js/google_map_field_previews.js',
        drupal_get_path('module', 'google_map_field') . '/js/google_map_field_setter.js',
      ),
      'css' => array(
        drupal_get_path('module', 'google_map_field') . '/css/google_map_field.css',
      ),
    ),
  );
  $defaults = array(
    'name' => isset($instance['default_value'][0]['name']) ? $instance['default_value'][0]['name'] : t('Default'),
    'lat' => isset($instance['default_value'][0]['lat']) ? $instance['default_value'][0]['lat'] : 51.524295,
    'lon' => isset($instance['default_value'][0]['lon']) ? $instance['default_value'][0]['lon'] : -0.1279899999999543,
    'zoom' => isset($instance['default_value'][0]['zoom']) ? $instance['default_value'][0]['zoom'] : 9,
  );
  $name = isset($items[$delta]['name']) ? $items[$delta]['name'] : '';
  $lat = isset($items[$delta]['lat']) ? $items[$delta]['lat'] : '';
  $lng = isset($items[$delta]['lon']) ? $items[$delta]['lon'] : '';
  $zoom = isset($items[$delta]['zoom']) ? $items[$delta]['zoom'] : '';
  $element['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Map Name'),
    '#default_value' => $name,
    '#prefix' => '<div class="google-map-field-fields-container">',
    '#attributes' => array(
      'data-name-delta' => $delta,
      'data-name-field-id' => $field_id,
    ),
  );
  $element['lat'] = array(
    '#type' => 'textfield',
    '#title' => t('Latitude'),
    '#required' => $instance['required'],
    '#element_validate' => array(
      'google_map_field_latlon_validate',
    ),
    '#default_value' => $lat,
    '#attributes' => array(
      'data-lat-delta' => $delta,
      'data-lat-field-id' => $field_id,
      'class' => array(
        'google-map-field-watch-change',
      ),
    ),
  );
  $element['lon'] = array(
    '#type' => 'textfield',
    '#title' => t('Longitude'),
    '#required' => $instance['required'],
    '#element_validate' => array(
      'google_map_field_latlon_validate',
    ),
    '#default_value' => $lng,
    '#attributes' => array(
      'data-lng-delta' => $delta,
      'data-lng-field-id' => $field_id,
      'class' => array(
        'google-map-field-watch-change',
      ),
    ),
  );
  $element['zoom'] = array(
    '#type' => 'textfield',
    '#title' => t('Zoom'),
    '#default_value' => $zoom,
    '#attributes' => array(
      'data-zoom-delta' => $delta,
      'data-zoom-field-id' => $field_id,
      'class' => array(
        'google-map-field-watch-change',
      ),
    ),
  );
  $element['enter_defaults'] = array(
    '#type' => 'button',
    '#value' => t('Defaults'),
    '#attributes' => array(
      'id' => 'enter_defaults_' . $field_id . '_' . $delta,
      'data-delta' => $delta,
      'data-field-id' => $field_id,
      'data-default-name' => $defaults['name'],
      'data-default-lat' => $defaults['lat'],
      'data-default-lon' => $defaults['lon'],
      'data-default-zoom' => $defaults['zoom'],
      'class' => array(
        'google-map-field-defaults',
      ),
    ),
  );
  $element['clear_fields'] = array(
    '#type' => 'button',
    '#value' => t('Clear Fields'),
    '#attributes' => array(
      'data-delta' => $delta,
      'data-field-id' => $field_id,
      'id' => 'clear_fields_' . $field_id . '_' . $delta,
      'class' => array(
        'google-map-field-clear',
      ),
    ),
    '#suffix' => '</div>',
  );
  $element['map_preview_' . $delta] = array(
    '#type' => 'container',
    '#prefix' => '<div class="google-map-field-setter-container">',
    '#suffix' => '<div><strong>' . t('Map Preview') . '</strong></div><div class="google-map-field-preview" date-field-name="' . $field['field_name'] . '" data-field-id="' . $field_id . '" data-delta="' . $delta . '"></div>',
  );
  $element['open_map'] = array(
    '#type' => 'button',
    '#value' => t('Set Map Marker'),
    '#attributes' => array(
      'data-delta' => $delta,
      'data-field-id' => $field_id,
      'id' => 'map_setter_' . $field_id . '_' . $delta,
      'data-field-name' => $field['field_name'],
    ),
    '#suffix' => '</div><div style="clear: both;"></div>',
  );
  return $element;
}

/**
 * Custom validation function for latitude/longitude fields.
 */
function google_map_field_latlon_validate($element, $form, &$form_state) {
  $invalid = FALSE;
  switch ($element['#parents'][count($element['#parents']) - 1]) {
    case 'lat':
      if ($element['#value'] < -90 || $element['#value'] > 90) {
        $invalid = TRUE;
      }
      break;
    case 'lon':
      if ($element['#value'] < -180 || $element['#value'] > 180) {
        $invalid = TRUE;
      }
      break;
  }
  if (!is_numeric($element['#value']) && !empty($element['#value']) || $invalid) {
    $field = implode('][', $element['#array_parents']);
    form_set_error($field, t('Invalid value - @title', array(
      '@title' => $element['#title'],
    )));
  }
}

/**
 * Implements hook_theme().
 */
function google_map_field_theme($existing, $type, $theme, $path) {
  if ($type == 'module') {
    return array(
      'google_map_field' => array(
        'variables' => array(
          'name' => NULL,
          'lat' => NULL,
          'lng' => NULL,
          'zoom' => NULL,
        ),
        'template' => 'google-map-field',
        'path' => drupal_get_path('module', 'google_map_field') . '/theme',
      ),
    );
  }
  return array();
}

/**
 * Helper function to add the Google Maps API.
 */
function google_map_field_add_maps_api() {
  if (variable_get('google_map_field_apikey', '') != '') {
    $element = array(
      '#type' => 'markup',
      '#markup' => '<script type="text/javascript" src="' . google_map_field_get_protocol() . 'maps.googleapis.com/maps/api/js?key=' . variable_get('google_map_field_apikey', '') . '"></script>',
    );
    drupal_add_html_head($element, 'google_maps_api');
  }
  else {
    drupal_add_js(google_map_field_get_protocol() . 'maps.googleapis.com/maps/api/js', 'external');
  }
}

/**
 * Helper function to return all fields created on the system
 * of the google map field type.
 */
function google_map_field_get_fields() {
  $types = array_keys(google_map_field_field_info());
  $fields = array();
  foreach (field_info_fields() as $field) {
    if (in_array($field['type'], $types)) {
      $fields[] = $field;
    }
  }
  return $fields;
}

/**
 * Helper function to return current protocol.
 */
function google_map_field_get_protocol() {
  return !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ? "https://" : "http://";
}

Functions

Namesort descending Description
google_map_field_add_maps_api Helper function to add the Google Maps API.
google_map_field_field_formatter_info Implements hook_field_formatter_info().
google_map_field_field_formatter_view Implements hook_field_formatter_view().
google_map_field_field_info Implements hook_field_info().
google_map_field_field_is_empty Implements hook_field_is_empty().
google_map_field_field_widget_error Implements hook_field_widget_error().
google_map_field_field_widget_form Implements hook_field_widget_form().
google_map_field_field_widget_info Implements hook_field_widget_info().
google_map_field_format_field This function formats the google map field for display, called by google_map_field_field_formatter_view().
google_map_field_get_fields Helper function to return all fields created on the system of the google map field type.
google_map_field_get_protocol Helper function to return current protocol.
google_map_field_help Implements hook_help().
google_map_field_latlon_validate Custom validation function for latitude/longitude fields.
google_map_field_menu Implements hook_menu().
google_map_field_permission Implements hook_permission().
google_map_field_theme Implements hook_theme().