function geolocation_googlemaps_field_widget_form in Geolocation Field 7
Implements hook_field_widget_form().
File
- modules/
geolocation_googlemaps/ geolocation_googlemaps.module, line 366 - Google Maps widget and formatters for Geolocation.
Code
function geolocation_googlemaps_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
// In order to make Geolocation field work with the popular Field collection
// module we check if our map widget is part of a Field collection and add the
// #field_parents delta.
if ($instance['entity_type'] == 'field_collection_item') {
$depth = count($element['#field_parents']) - 1;
$parent_delta = $element['#field_parents'][$depth];
$id = $instance['id'] . '-' . $parent_delta . '-' . $delta;
}
else {
$id = $instance['id'] . '-' . $delta;
}
$lat_value = isset($items[$delta]['lat']) ? $items[$delta]['lat'] : NULL;
// To make this widget compatible with geofiled we need to rename the keys for
// longitude. Geofield uses "lon" while Geolocation Field uses "lng".
if ($field['type'] == 'geofield') {
$lng_value = isset($items[$delta]['lon']) ? $items[$delta]['lon'] : NULL;
}
else {
$lng_value = isset($items[$delta]['lng']) ? $items[$delta]['lng'] : NULL;
}
$element += array(
'#delta' => $delta,
);
switch ($instance['widget']['type']) {
case 'geolocation_googlemap':
$element['address'] = array(
'#type' => 'item',
'#title' => $element['#title'],
'#prefix' => '<div id="geolocation-address-' . $id . '" class="geolocation-address">',
'#suffix' => '</div>',
'#required' => $instance['required'],
);
$element['address']['field'] = array(
'#type' => 'textfield',
'#maxlength' => 120,
);
$element['address']['geocode'] = array(
'#prefix' => '<span id="geolocation-address-geocode-' . $id . '" class="geolocation-address-geocode">',
'#suffix' => '</span>',
'#markup' => t('Get location'),
);
$element['address']['geocode-suggestions'] = array(
'#prefix' => '<div id="geolocation-address-geocode-suggestions-' . $id . '" class="geolocation-address-geocode-suggestions">',
'#suffix' => '</div>',
'#markup' => '<label>' . t('Suggestions') . '</label><div class="suggestion-options">' . t('Not found') . '</div>',
);
$element['help'] = array(
'#prefix' => '<div id="geolocation-help-' . $id . '" class="geolocation-help">',
'#suffix' => '</div>',
'#markup' => t('Enter an address / location / Google map URL in the textfield or you can also click on the map to set a marker'),
);
$element['googlemap'] = array(
'#prefix' => '<div id="geolocation-map-' . $id . '" class="geolocation-map" style="width:100%;height:400px;">',
'#suffix' => '</div>',
);
// Presentational item.
$element['latitem'] = array(
'#type' => 'item',
'#title' => t('Latitude:'),
'#prefix' => '<div id="geolocation-lat-item-' . $id . '" class="geolocation-lat-item">',
'#suffix' => '</div>',
'#markup' => '<span class="geolocation-lat-item-value">' . $lat_value . '</span>',
'#required' => $instance['required'],
);
$element['lat'] = array(
'#type' => 'hidden',
'#prefix' => '<div id="geolocation-lat-' . $id . '" class="geolocation-lat">',
'#suffix' => '</div>',
'#default_value' => $lat_value,
);
// Presentational item.
$element['lngitem'] = array(
'#type' => 'item',
'#title' => t('Longitude:'),
'#prefix' => '<div id="geolocation-lng-item-' . $id . '" class="geolocation-lng-item">',
'#suffix' => '</div>',
'#markup' => '<span class="geolocation-lat-item-value">' . $lat_value . '</span>',
'#required' => $instance['required'],
);
$element['lng'] = array(
'#type' => 'hidden',
'#prefix' => '<div id="geolocation-lng-' . $id . '" class="geolocation-lng">',
'#suffix' => '</div>',
'#default_value' => $lng_value,
);
$element['remove'] = array(
'#prefix' => '<div id="geolocation-remove-' . $id . '" class="geolocation-remove"><span>',
'#suffix' => '</span></div>',
'#markup' => t('Remove'),
);
// Attach CSS and JS files via FAPI '#attached'.
$element['googlemap']['#attached']['css'][] = drupal_get_path('module', 'geolocation_googlemaps') . '/geolocation_googlemaps.css';
$element['googlemap']['#attached']['js'][] = array(
'data' => drupal_get_path('module', 'geolocation_googlemaps') . '/geolocation_googlemaps_widget.js',
'type' => 'file',
'scope' => 'footer',
);
geolocation_googlemaps_attach_google_js($element);
// Make defaults available as javascript settings. In JS files use:
// Drupal.settings.mapDefaults.lat.
$map_defaults_lat = $lat_value ? $lat_value : '';
$map_defaults_lng = $lng_value ? $lng_value : '';
$map_defaults = array(
$id => array(
'lat' => $map_defaults_lat,
'lng' => $map_defaults_lng,
),
);
$data = array(
'defaults' => $map_defaults,
'settings' => $instance['widget']['settings'],
);
$element['googlemap']['#attached']['js'][] = array(
'data' => array(
'geolocation' => $data,
),
'type' => 'setting',
);
$element['field_type'] = array(
'#type' => 'value',
'#value' => $field['type'],
);
$element['#element_validate'] = array(
'geolocation_googlemaps_field_widget_validate',
);
$element['#element_validate'][] = 'geolocation_googlemaps_field_widget_set_value';
break;
}
return $element;
}