The location_gmap_find_address submodule contains a check to see if it should include the "Find address on map" button, but this check excludes the user profile form. Since location fields can now be added to users, this line needs amendment.

I changed line 7 of location_gmap_find_address.module from
if (isset($form['#node_edit_form']) && $form['#node_edit_form'] && variable_get('location_usegmap', 0)) {
to
if ($form['#form_id'] == 'user_profile_form' || (isset($form['#node_edit_form']) && $form['#node_edit_form']) && variable_get('location_usegmap', 0)) {

This made the button appear. I don't know if this is the best way of addressing this issue but as I couldn't find it referred to anywhere I thought I would post it here and see if anyone can say if this is a problem or should be made into a patch.

Comments

alessandro rancati’s picture

Did not work for me. When I replace the code I get the following errors:

Notice: Undefined index: #entity_type a location_gmap_find_address_form_alter() (line 8 of /sites/all/modules/gmap/location_gmap_find_address/location_gmap_find_address.module).
Notice: Undefined index: #bundle a location_gmap_find_address_form_alter() (line 8 of sites/all/modules/gmap/location_gmap_find_address/location_gmap_find_address.module).
Notice: Undefined index: widget a location_gmap_find_address_form_alter() (line 11 of sites/all/modules/gmap/location_gmap_find_address/location_gmap_find_address.module).
Notice: Undefined index: widget a location_gmap_find_address_form_alter() (line 11 of sites/all/modules/gmap/location_gmap_find_address/location_gmap_find_address.module).
Notice: Undefined index: widget a location_gmap_find_address_form_alter() (line 11 of sites/all/modules/gmap/location_gmap_find_address/location_gmap_find_address.module).
Notice: Undefined index: widget a location_gmap_find_address_form_alter() (line 11 of sites/all/modules/gmap/location_gmap_find_address/location_gmap_find_address.module).
Notice: Undefined index: widget a location_gmap_find_address_form_alter() (line 11 of sites/all/modules/gmap/location_gmap_find_address/location_gmap_find_address.module).
Notice: Undefined index: widget a location_gmap_find_address_form_alter() (line 11 of sites/all/modules/gmap/location_gmap_find_address/location_gmap_find_address.module).
Notice: Undefined index: widget a location_gmap_find_address_form_alter() (line 11 of sites/all/modules/gmap/location_gmap_find_address/location_gmap_find_address.module).
Notice: Undefined index: widget a location_gmap_find_address_form_alter() (line 11 of sites/all/modules/gmap/location_gmap_find_address/location_gmap_find_address.module).
Notice: Undefined index: widget a location_gmap_find_address_form_alter() (line 11 of sites/all/modules/gmap/location_gmap_find_address/location_gmap_find_address.module).
Notice: Undefined index: widget a location_gmap_find_address_form_alter() (line 11 of sites/all/modules/gmap/location_gmap_find_address/location_gmap_find_address.module).
Notice: Undefined index: widget a location_gmap_find_address_form_alter() (line 11 of sites/all/modules/gmap/location_gmap_find_address/location_gmap_find_address.module).
Notice: Undefined index: widget a location_gmap_find_address_form_alter() (line 11 of sites/all/modules/gmap/location_gmap_find_address/location_gmap_find_address.module).
Notice: Undefined index: widget a location_gmap_find_address_form_alter() (line 11 of sites/all/modules/gmap/location_gmap_find_address/location_gmap_find_address.module).

If I replace the
$form['#form_id'] == 'user_profile_form'
with
$form['#form_id'] == 'user-profile-form'
The errors disappear but the button is still missing.
I am running drupal 7.26 with gmap 7.x-2.8 and Location 7.x-3.2-beta2

donutdan4114’s picture

This issue isn't just for the user profile form.
The button won't show up anywhere else, aside from the node edit form.

Doesn't work with Inline Entity Form module.

DrCord’s picture

I was able to make this work for a user profile2 location field. It required some custom code, but really this should be expanded to work with all profile types and then added to this module. This code is setup to work with a profile2 profile type called "personal_information", change that to your profile type machine name.

New location_gmap_find_address.module file code


/**
 * hook_form_alter() override
 */
function location_gmap_find_address_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['#node_edit_form']) && $form['#node_edit_form'] && variable_get('location_usegmap', 0)) {
    $fields = field_info_instances($form['#entity_type'], $form['#bundle']);
    $form['#location_gmap_find_address_fields'] = array();
    foreach ($fields as $field_name => $field) {
      if ($field['widget']['type'] == 'location') {
        $form['#location_gmap_find_address_fields'][] = $field_name;
      }
    }
    if (count($form['#location_gmap_find_address_fields'])) {
      $form["#after_build"][] = "location_gmap_find_address_after_build";
    }
  }
  //patch for user location gmap field on personal_information profile 'shard'/section
  //Feynman Group - Cord Slatton-Valle - 12/19/14
  if ($form['#form_id'] == 'user_profile_form') {
    $fields = field_info_instances('profile2', 'personal_information');
    $form['#location_gmap_find_address_fields'] = array();
    foreach ($fields as $field_name => $field) {
      if (isset($field['widget']['type']) && $field['widget']['type'] == 'location') {
        $form['#location_gmap_find_address_fields'][] = $field_name;
      }
    }
    if (count($form['#location_gmap_find_address_fields'])) {
      $form["#after_build"][] = "location_gmap_find_address_after_build_profile";
    }
  }
}

/**
 * After build event for this form
 */
function location_gmap_find_address_after_build($form, &$form_state) {

  foreach ($form['#location_gmap_find_address_fields'] as $field_name) {
    $field_instance_count = $form[$field_name]['und']['#max_delta'];
    for ($i = 0; $i <= $field_instance_count; $i++) {
      if (isset($form[$field_name]['und'][$i]['locpick'])) {
        $form[$field_name]['und'][$i]['locpick']["#prefix"] = '<div class="location-gmap-find-address-button-wrapper"><button type="button" class="location-gmap-find-address-button" value="' . str_replace('_', '-', $field_name) . '-und-' . $i . '">' . t('Find Address on Map') . '</button></div>';
      }
    }
  }
  drupal_add_js(drupal_get_path("module", "location_gmap_find_address") . "/location_gmap_find_address.js");

  return $form;
}

function location_gmap_find_address_after_build_profile($form, &$form_state) {
  //custom function - modeled from location_gmap_find_address_after_build function
  // Feynman Group - Cord Slatton-Valle - 12/19/14
  foreach ($form['#location_gmap_find_address_fields'] as $field_name) {
    $field_instance_count = 0;
    for ($i = 0; $i <= $field_instance_count; $i++) {
      if (isset($form['profile_personal_information'][$field_name]['und'][$i]['locpick'])) {
        $form['profile_personal_information'][$field_name]['und'][$i]['locpick']["#prefix"] = '<div class="location-gmap-find-address-button-wrapper"><button type="button" class="location-gmap-find-address-button" value="' . str_replace('_', '-', $field_name) . '-und-' . $i . '">' . t('Find Address on Map') . '</button></div>';
      }
    }
  }
  drupal_add_js(drupal_get_path("module", "location_gmap_find_address") . "/location_gmap_find_address_profile.js");

  return $form;
}

location_gmap_find_address_profile.js : New additional JavaScript file that works with the "personal_information" profile2 profile type, change this to work with your profile type machine name.

(function ($) {
    Drupal.behaviors.mjh_views_alter = {
        attach: function () {

            $("button.location-gmap-find-address-button").click(function (e) {
                e.preventDefault();
                var address_parts = new Array();
                $("fieldset#edit-profile-personal-information-" + $(this).val() + " .form-item input[type=text]," +
                    "fieldset#edit-profile-personal-information-" + $(this).val() + " .form-item select > option:selected").each(function () {
                        if (!$(this).hasClass('gmap-control') && $(this).val() != '') {
                            if ($(this).is('option')) {
                                address_parts.push($(this).text());
                            } else {
                                address_parts.push($(this).val());
                            }
                        }
                    });

                var address_string = address_parts.join(', ');
                var gmap_id = $("fieldset#edit-profile-personal-information-" + $(this).val() + " .gmap-map").attr('id');
                if (google.maps.version !== 'undefined') { // assume Google Maps API v3 as API v2 did not have this variable
                    var geocoder = new google.maps.Geocoder();
                    geocoder.geocode({'address': address_string}, function (results, status) {
                        if (status == google.maps.GeocoderStatus.OK) {
                            var m = Drupal.gmap.getMap(gmap_id);
                            m.locpick_coord = results[0].geometry.location;
                            m.change('locpickchange');
                            m.map.setCenter(results[0].geometry.location);
                            m.map.setZoom(14);
                        }
                        else {
                            alert(Drupal.t("Your address was not found."));
                        }
                    });
                }
                else {
                    var geocoder = new GClientGeocoder();
                    geocoder.reset(); // Clear the client-side cache
                    geocoder.getLatLng(address_string, function (point) {
                        if (!point) {
                            alert(Drupal.t("Your address was not found."));
                        }
                        else {
                            var m = Drupal.gmap.getMap(gmap_id);
                            m.locpick_coord = point;
                            m.change('locpickchange');
                            m.map.setCenter(point, 14);

                        }
                    });
                }

            });

        }

    }
})(jQuery);


saranya mohan’s picture

But this button is appearing at the top of a profile form. it just appear on top of the location field.

saranya mohan’s picture

user can enter only one location find address button appears correctly. but it has add multiple location this code is not wroking properly

giuvax’s picture

After three years ths button is still showing just on nodes. Is anybody working at this?