If you need to modify the Location module's form on a node, try something like this:
function my_module_form_alter (&$form, &$form_state, $form_id) {
switch ($form_id) {
case "my_node_form":
$location = array(
'street' => '123 Fake Street',
'city' => 'Ann Arbor',
'province' => 'MI',
'postal_code' => '48123',
'country' => 'us',
'province_name' => 'Michigan',
'country_name' => 'United States',
'latitude' => '42.27634',
'longitude' => '-83.73782',
'locpick' = array(
'user_latitude' => '42.27634',
'user_longitude' => '-83.73782'
)
);
$form['locations'][0]['#default_value'] = $location;
break;
}
}
In the example above, I am setting the default form inputs for a node's location.
Additionally for my use case, I needed to prevent the user from changing the location form input values on the node edit form since this data was being dynamically loaded from an external source. I was unable to use the #disabled propertly on the location form elements during hook_form_alter and #after_build, so I used some JQuery instead from hook_form_alter:
// add javascript to disable the location form elements
// (was unable to use hook_form_alter and/or #after_build to disable these)
$disable_js =
"$('#edit-locations-0-street').attr('disabled', 'disabled');" .
"$('#edit-locations-0-additional').attr('disabled', 'disabled');" .
"$('#edit-locations-0-city').attr('disabled', 'disabled');" .
"$('#edit-locations-0-province').attr('disabled', 'disabled');" .
"$('#edit-locations-0-postal-code').attr('disabled', 'disabled');" .
"$('#gmap-auto1map-locpick_latitude0').attr('disabled', 'disabled');" .
"$('#gmap-auto1map-locpick_longitude0').attr('disabled', 'disabled');";
drupal_add_js($disable_js,"inline","footer");
// add javascript to re-enable the location form elements before submission,
// otherwise the values will not be saved to the node
$enable_js =
"$('form#node-form').submit(function(){" .
"$('#edit-locations-0-street').removeAttr('disabled');" .
"$('#edit-locations-0-additional').removeAttr('disabled');" .
"$('#edit-locations-0-city').removeAttr('disabled');" .
"$('#edit-locations-0-province').removeAttr('disabled');" .
"$('#edit-locations-0-postal-code').removeAttr('disabled');" .
"$('#gmap-auto1map-locpick_latitude0').removeAttr('disabled');" .
"$('#gmap-auto1map-locpick_longitude0').removeAttr('disabled');" .
"});";
drupal_add_js($enable_js,"inline","footer");
Add new comment