Steps to reproduce:

  1. Instal Drupal 7.21 with Ctools 7.x-1.2, Entity 7.x-1.0, Addressfield 7.x-1.0-beta3, Profile2 7.x-1.3, GeoPHP 7.x-1.7, Geocoder 7.x-1.2, Openlayers 7.x-2.0-beta3 and Geofield 7.x-2.x-dev;
  2. Add adressfield called Address to provided Profile2 profile (Main Profile);
  3. Add geofield called Location to provided Profile2 profile (Main Profile) and choose wiget type OpenLayers Map;
  4. Configure Location field with following values:
    • Store each simple feature as a separate field;
    • Allow only Point in Available Features;
    • Enable geocoding of location data;
    • Geocode from created addressfield;
    • Google Geocoder with default options;
  5. Go to user Profile2 page, fill Address field and save;
  6. Go to the same user Profile2 page and click button "Find using Adress field".

What should happen:

Map has a new marker in the geographical coordinates corresponding to the address we used.

What happens:

Nothing and watchdog throws error:

Notice: Undefined index: field_address in openlayers_use_geocoder_submit() (line 329 of /Users/guilherme/Sites/drupal/sites/all/modules/geofield/geofield.widgets.openlayers.inc).

followed by:

User error: geocoder_widget_get_field_value: You must pass either $source_field_values OR $entity in geocoder_widget_get_field_value() (line 183 of /Users/guilherme/Sites/drupal/sites/all/modules/geocoder/geocoder.widget.inc).

Aditional Notes:

Repeating steps 3 to 6 on a regular content type works as expected.
Tested on a Macbook Pro with Mac OSX 10.8.2, Apache 2.2.22, PHP 5.4.11 and Maria 5.5.29 after first noticing this issue on a VPS with Debian Squeeze 6.07, Apache 2.2.16, PHP 5.3.3 and MySQL 5.1.66
Odd behavior with Profile2 and Geofield was first noticed after trying to get Leaflet Widget and Geofield Gmap to work with Geofied 7.x-1.1 for fields in Profile2 pages and not succeeding.

Comments

sano’s picture

Possibly related to this issue: http://drupal.org/node/1963242

finn lewis’s picture

I have the same behaviour, with the same watchdog message.
I read through https://drupal.org/node/1963242 and tried the latest Geofield 7.x-2.x-dev but still get the same behaviour on profile2 entities.

eugenesia’s picture

It seems the structure of a user profile edit form is different from that of a node edit form. The widget's submit handler works for a node edit form but fails when it's a user profile editing form for a profile2 profile. The buggy function is openlayers_use_geocoder_submit() in the file geofield.widgets.openlayers.inc.

My solution was to replace the buggy submit handler using a hook. I'm sharing my workaround in case others find it useful. I implemented the hook in a module named mymodule.


/**
 * Implements hook_field_widget_form_alter().
 *
 * Geocoding in OpenLayers widget doesn't work for profile2 fields, see: https://drupal.org/node/1938632
 * The bug is in the widget's submit handler, so replace it.
 */
function mymodule_field_widget_form_alter(&$element, &$form_state, $context) {

  // Only do stuff if the geocoding widget is being used, and if the geofield is part of a profile2 entity.
  if ($context['instance']['widget']['type'] == 'geofield_openlayers' && $context['instance']['entity_type'] == 'profile2') {

      $submit_handlers =& $element[0]['use_geocoder']['#submit'];
      $handler_key = array_search('openlayers_use_geocoder_submit', $submit_handlers);

      if ($handler_key !== FALSE) {
        // This is our corrected form submit handler.
        $submit_handlers[$handler_key] = 'mymodule_use_geocoder_submit';
      }
    }
  }
}

The function below is our amended widget submit function handler. In the 'modified' section, I've changed $form_state['values'][$geocoder_field][$langcode] to $form_state['input'][$parents[0]][$geocoder_field][$langcode]. It seems to work for this case, but I haven't tested it thoroughly. I'd appreciate any inputs you have.

/**
 * Replacement widget form submit handler for the buggy openlayers_use_geocoder_submit(). 
 * Everything copied from the function except for the part in the 'Modified section'.
 */
function mymodule_use_geocoder_submit($form, &$form_state) {

  $button = $form_state['triggering_element'];

  // Go one level up in the form, to the widgets container.
  $element = drupal_array_get_nested_value($form, array_slice($button['#array_parents'], 0, -1));

  $field_name = $element['#field_name'];
  $langcode = $element['#language'];
  $delta = $element['#delta'];
  $parents = $element['#field_parents'];

  // Set the widget value based on geocoding results.
  $field_state = field_form_get_state($parents, $field_name, $langcode, $form_state);

  $geocoder_field = $field_state['instance']['widget']['settings']['geocoder_field'];

  if ($field_value = geocoder_widget_get_field_value($element['#entity_type'], $field_state['instance'], NULL, 

    // ---------------------
    // Modified section starts.
    // ---------------------

    // Original expression - only works if it's not a profile2 entity.
    // $form_state['values'][$geocoder_field][$langcode])) {

    // New expression - uses name of the widget's parent element $parents[0],
    // e.g. if my profile2 name is 'business', $parents[0] is 'profile_business'.
    $form_state['input'][$parents[0]][$geocoder_field][$langcode])) {

    // ---------------------
    // Modified section ends.
    // ---------------------

    geophp_load();
    $geometry = geoPHP::load($field_value[$langcode][$delta]['geom']);

    // OpenLayers can only use WKT, so translate
    $field_value[$langcode][$delta]['geom'] = $geometry->out('wkt');
    $field_value[$langcode][$delta]['input_format'] = 'wkt';

    // Override the field's value in the 'input' array to substitute the new
    // field value for the one that was submitted.
    drupal_array_set_nested_value($form_state, array_merge(array('values'), $parents, array($field_name)), $field_value);
  }
}
davidpugh’s picture

I had this same problem. I fixed it by uninstalling geofields 7.x-2.1
and installing geofields 7.x-2.0.
My geofield was not autofilling with "Geocode from another field" on my new Profile2 field.

I changed geofields 7.x-2.1 to 7.x-2.0 and now my "geocode from another field" field on my new Profile2 profile
is getting populated automatically with my geo location data.

There is something wrong with the new geofields 7.x-2.1 version.