function geolocation_googlemaps_widget in Geolocation Field 6
Implementation of hook_widget().
Attach a single form element to the form.
CCK core fields only add a stub element and builds the complete item in #process so reusable elements created by hook_elements can be plugged into any module that provides valid $field information.
Custom widgets that don't care about using hook_elements can be built out completely at this time.
If there are multiple values for this field and CCK is handling multiple values, the content module will call this function as many times as needed.
Parameters
$form: the entire form array, $form['#node'] holds node information
$form_state: the form_state, $form_state['values'][$field['field_name']] holds the field's form values.
$field: the field array
$items: array of default values for this field
$delta: the order of this item in the array of subelements (0, 1, 2, etc)
Return value
the form item for a single element for this field
File
- modules/
geolocation_googlemaps/ geolocation_googlemaps.module, line 153
Code
function geolocation_googlemaps_widget(&$form, &$form_state, $field, $items, $delta = 0) {
$lat_value = isset($items[$delta]['lat']) ? $items[$delta]['lat'] : '';
$lng_value = isset($items[$delta]['lng']) ? $items[$delta]['lng'] : '';
$element['lat'] = array();
$element['lng'] = array();
$element['googlemap'] = array();
$element['lat'] += array(
'#type' => 'hidden',
'#prefix' => '<div id="lat-' . $delta . '-field" class="address">',
'#suffix' => '</div>',
'#attributes' => array(
'class' => 'geolocation-lat',
),
'#default_value' => $lat_value,
'#maxlength' => 30,
);
$element['lng'] += array(
'#type' => 'hidden',
'#prefix' => '<div id="lng-' . $delta . '-field" class="address">',
'#suffix' => '</div>',
'#attributes' => array(
'class' => 'geolocation-lng',
),
'#default_value' => $lng_value,
'#maxlength' => 30,
);
$element['address'] = array(
'#type' => 'textfield',
'#title' => t('Geolocation'),
'#prefix' => '<div id="address-' . $delta . '" class="address">',
'#suffix' => '</div>',
'#attributes' => array(
'class' => 'geolocation-input address',
),
'#maxlength' => 120,
'#weight' => -5.1,
'#description' => t('Enter an address / location in the textfield above. You can also right-click on the map to set a marker'),
);
$element['setaddress'] = array(
'#prefix' => '<div id="map-' . $delta . '-setaddress">',
'#suffix' => '</div>',
'#value' => t('Set location'),
'#weight' => -5,
);
// Copied from Google examples:
// http://demos.projectx.co.nz/gmaps3/reverse_geocoder.html
$element['googlemap'] = array(
'#prefix' => '<div id="map-' . $delta . '" class="map" style="width:100%;height:400px;">',
'#suffix' => '</div>',
'#value' => ' ',
);
$element['remove'] = array(
'#prefix' => '<div id="map-' . $delta . '-remove">',
'#suffix' => '</div>',
'#value' => t('Remove'),
);
// Attach CSS and JS files via FAPI '#attached'.
drupal_add_css(drupal_get_path('module', 'geolocation_googlemaps') . '/geolocation_googlemaps.css');
$element['googlemap']['#attached']['css'][] = drupal_get_path('module', 'geolocation_googlemaps') . '/geolocation_googlemaps.css';
drupal_add_js(drupal_get_path('module', 'geolocation_googlemaps') . '/geolocation_googlemaps.js');
// Make defaults available as javascript settings. In JS files use:
// Drupal.settings.map_defaults.lat
// TODO: better defaults.
$map_defaults_lat = $lat_value == '' ? 49.9148115245017 : $lat_value;
$map_defaults_lng = $lng_value == '' ? 10.8783125877381 : $lng_value;
$map_defaults = array(
$delta => array(
'lat' => $map_defaults_lat,
'lng' => $map_defaults_lng,
),
);
drupal_add_js(array(
'map_defaults' => $map_defaults,
), 'setting');
return $element;
}