This module creates a new widget type for the Address Field. It allows populating an Address Field from a Geofield lat/lon pair, using Google Maps API Reverse Geocoding capability.

A simple use case is the following:
- field_input is a Text field, use to type in an address, using "Geocoder field with autocomplete" widget.
- field_coord is a GeoField, which will store the geodata in a Lat/Lon pair format.
- field_address is an AddressField, using this module's "Populate from Geofield" widget.

From any input (ex: Empire State Building), Geofield module will retrieve Lat/Lon coordinate, then this module will retrieve a full administrative address using Google Api (city, country, street name, etc.).

Project page

https://drupal.org/sandbox/rcuny/2160073

Repository

git clone --branch 7.x http://git.drupal.org/sandbox/rcuny/2160073.git addressfield_from_geofield

Comments

pachabhaiya’s picture

Status: Needs review » Needs work

Dear renaudcuny,

Please use this online tool (pareview.sh) to check for general errors in your module code.

Checking your code using this tool shows lots of errors.
http://pareview.sh/pareview/httpgitdrupalorgsandboxrcuny2160073git

Please take your time to correct those issues.

Cheers!!!

renaudcuny’s picture

Dear Chhabi,

Thanks for your time and comment.

I've fixed all the errors and most of the warnings, only a few warnings remain, about long lines.
http://pareview.sh/pareview/httpgitdrupalorgsandboxrcuny2160073git

Sorry for that, it's my first contrib module submission. Hope I got it right this time! :)
Cheers,
Renaud

adam_thomason’s picture

Hi,

After reviewing some of your code, I have a couple of suggestions to improve your code.

Beginning at line 121:

  $info = "";
  foreach ($address->results as $address_info) {
    $info = _addressfield_from_geofield_extract_address_info_value($address_info, $request);
    if ($info != "") {
      break;
    }
  }

The if() statement should be replaced with this:

if (!empty($info)) {
    break;
}

There are several more instances of this. It is better practice to check for empty strings with empty(), rather than comparing empty strings.

Other than these small items, there are no noticeable errors with your code. Keep up the good work.

Cheers,
Adam

renaudcuny’s picture

Hi Adam,

Thanks for the review!

Indeed, it's always hard to get rid of old bad coding habits :). I've updated the code using the empty() function.

I wish you a nice weekend,
Cheers,

Renaud

renaudcuny’s picture

Status: Needs work » Needs review
PA robot’s picture

We are currently quite busy with all the project applications and we prefer projects with a review bonus. Please help reviewing and put yourself on the high priority list, then we will take a look at your project right away :-)

Also, you should get your friends, colleagues or other community members involved to review this application. Let them go through the review checklist and post a comment that sets this issue to "needs work" (they found some problems with the project) or "reviewed & tested by the community" (they found no major flaws).

I'm a robot and this is an automated message from Project Applications Scraper.

nitesh sethia’s picture

Hey,

Make a new branch called as 7.x and push the code there. Also don't forget to remove the master branch.

hayashi’s picture

Hi @renaudcuny,

I reviewed you code, I found a using file_get_contents().
It's better to using drupal_http_request() instead of file_get_contents() to work with servers behind proxy.

renaudcuny’s picture

Issue summary: View changes
renaudcuny’s picture

Hi,
Thanks you 2 for your reviews and comment.
I've made the branch name change, and I'll tale care of @hayashi's comment asap.
I keep you posted :)

renaudcuny’s picture

Issue summary: View changes
renaudcuny’s picture

Hi,
I replaced file_get_contents() by drupal_http_request(), and changed the surrounding logic accordingly (commit 5c36330), based on comment from @hayashi.
Thanks again for your review.

gobinathm’s picture

Status: Needs review » Needs work

renaudcuny there are few coding standards issue reported by pareview, can you address them http://pareview.sh/pareview/httpgitdrupalorgsandboxrcuny2160073git

renaudcuny’s picture

Hi Gobinathm,

Thanks for you review.
These are just warnings on line length, mostly inside comments or due to Google API calls (long addresses like "https://maps.googleapis.com/maps/api/geocode/json?latlng=....."). Is it really a blocking issue?

Cheers,
Renaud

renaudcuny’s picture

Status: Needs work » Needs review
gobinathm’s picture

@renaudcuny those where simple suggestion, not an application blocker

Project applications with Review bonus takes priority. So please complete manual reviews & include those url's here. Once done, please include appropriate tags in the application.

heddn’s picture

Status: Needs review » Needs work

Please remove .gitignore.

Use https://api.drupal.org/api/drupal/modules%21system%21system.api.php/func... rather than adjusting module weight.

function addressfield_from_geofield_field_attach_presave($entity_type, $entity) {
      if ($geofield_value) {
        // Hardcoded, should move to field settings. Modify according to your needs.
        $force_language = "fr";
        addressfield_from_geofield_set_address_from_geodata($entity, $field_name, $geofield_value, $force_language);
      }

Hard coded language of FR seems an issue. Maybe declare global $language and pull language from there.

global $language;
$language_code = $language->language;
      // if you want to erase the address data when geocoding fails, uncomment the following:
      /*
      else {
      $entity->{$field_name} = array();
      }
      */

Either make this a configurable option (somehow) or remove it. Its not a best practice to hack core or contrib modules.

function addressfield_from_geofield_set_address_from_geodata($entity, $field_name, $geofield_value, $lang = NULL) {
...
  if ($address) {
    // Extract the interesting pieces of information from the address data.
    $street_number = _addressfield_from_geofield_extract_address_info($address, "street_number");
    $route         = _addressfield_from_geofield_extract_address_info($address, "route");
    $postal_code   = _addressfield_from_geofield_extract_address_info($address, "postal_code");
    $locality      = _addressfield_from_geofield_extract_address_info($address, "locality");
    $country       = _addressfield_from_geofield_extract_address_info($address, "country");

Use single spaces around the equals. https://drupal.org/coding-standards#operators

function addressfield_from_geofield_set_address_from_geodata($entity, $field_name, $geofield_value, $lang = NULL) {
...
    // To store something in "Address 2" field, here it goes
    // $entity->{$field_name}[LANGUAGE_NONE][0]['premise'] = "address 2 field";

Comments like this should either be addressed or noted as a @todo.

function _addressfield_from_geofield_reverse_geocode($lat, $lon, $lang = NULL) {
  $json = NULL;
  $language_setting = ($lang) ? '&language=' . $lang : '';
  $response = drupal_http_request('https://maps.googleapis.com/maps/api/geocode/json?latlng=' . $lat . ',' . $lon . '&sensor=false' . $language_setting);
  if (!isset($response->error)) {
    $json = json_decode($response->data);
  }
  return $json;

Maybe check for HTTP 200 status code too?

heddn’s picture

Also all comments, including inline comments should be under 80 characters. See bullet #8: https://drupal.org/coding-standards/docs#drupal

renaudcuny’s picture

Hi heddn,
Thanks a lot for this very long review.
I'll take care of these issues as soon as I've a minute.
Cheers,
Renaud

PA robot’s picture

Status: Needs work » Closed (won't fix)

Closing due to lack of activity. If you are still working on this application, you should fix all known problems and then set the status to "Needs review". (See also the project application workflow).

I'm a robot and this is an automated message from Project Applications Scraper.