I noticed that you have an empty function named fboauth_field_convert_location() in fboauth.field.inc, so it seems like you thought about importing locations.
For my current project, I'm using the Address Field module to store locations, and the Geocoder module to store latitude and longitude coordinates. I modified fbouath to support importing data into an address field.
I'll just post the relevant snippets for now, given that I've made other changes (to support the name field). If you'd like to roll these changes into dev, just let me know and I'd be happy to submit a patch.
<?php
/**
* Facebook data conversion function.
*
* Converts an incoming Facebook location (which is an object) into an array compatible with the addressfield module.
*/
function fboauth_field_convert_location($facebook_property_name, $fbuser, $field, $instance) {
$value = NULL;
if ($field['type'] == 'addressfield' && module_exists('geocoder')) {
$geodata = geocoder('google', $fbuser->location->name);
foreach ($geodata->data['geocoder_address_components'] as $key => $components) {
switch($components->types[0]) {
// Set city.
case 'locality':
$value['locality'] = $components->short_name;
break;
// Set state.
case 'administrative_area_level_1':
$value['administrative_area'] = $components->short_name;
break;
// Set country.
case 'country':
$value['country'] = $components->short_name;
break;
}
}
}
return $value;
}
?>
Of course, I had to make changes to fboauth_user_properties() and fboauth_field_convert_info() as well, but those were pretty simple:
Modified $properties:
'location' => array(
'permission' => 'user_location',
'label' => t('Location'),
'field_types' => array('text', 'addressfield'),
),
Modified $convert_info, added:
'addressfield' => array(
'label' => t('Address Field'),
'callback' => 'fboauth_field_convert_location',
),
This does require geocoder to split the address into component parts. Furthermore, the coordinates are stored in the $geodata object, so you can very easily migrate those into a field with the same method.
| Comment | File | Size | Author |
|---|---|---|---|
| #8 | fboauth_properties_alter.patch | 2.8 KB | quicksketch |
| #7 | fboauth_properties_alter.patch | 376 bytes | quicksketch |
| #6 | fboauth_properties_alter.patch | 376 bytes | quicksketch |
Comments
Comment #1
quicksketchI don't think it's likely that we'll support location natively, though I'd be happy to add a hook into the module to allow modules to convert the location based on which location modules they had enabled.
Comment #2
thlor commented@madmatter23 I'm looking for the same functionality. How could I implement your code?
Comment #3
grasmash commented@quicksketch adding a hook sounds great.
Here's another option to consider. If you were to call drupal_alter() on $properties in fboauth_user_properties(), developers would then be able to:
1) Add new field_types to $properties in fboauth_user_properties().
2) Add new callbacks to $convert_info in fboauth_field_convert_info().
3) Define their own callbacks for data conversion.
This seems like a very flexible solution that would allow me to implement the above code without modifying fboauth.
Let me know what you think.
Thanks!
Comment #4
grasmash commented@thlor Just make the modifications to the three functions that I listed: fboauth_field_convert_location(), fboauth_user_properties() and fboauth_field_convert_info().
You can just replace fboauth_field_convert_location() in fboauth.field.inc.
The other two functions simply define arrays for mapping out user properties and callbacks. Add the 'addressfield' row to the $convert_info array in fboauth_field_convert_info(), and add the second 'addressfield' row to the field_types key under 'location' in $properties.
That being said, I don't think that this is the best way to do this. If quicksketch adds a new hook, or makes $properties available via drupal_alter(), then it will be much better for you to create a custom module that hooks fboauth. If that happens, I'll write a quick blog post on it and post a link here.
Comment #5
quicksketchI think this sounds like an excellent approach. I'm actually a little surprised I didn't include one in there by default. We already have hook_fboauth_field_convert_info(), so if a module were to add "location" or some other type into fboauth_user_properties(), they could specify a conversion mechanism in hook_fboauth_field_convert_info(). I was obviously thinking about this situation, but didn't complete the implementation since it wasn't relevant for me at the time.
Comment #6
quicksketchSo this patch is trivial, but I'm not sure it's 100% right. The addition of "include_common" property has me worried that 3rd party modules will have to deal with "sometimes there" properties. I'll see if I can come up with something a bit cleaner.
Comment #7
quicksketchThis patch keeps the common properties always available so they can be altered without fear, then diffs them back out after the drupal_alter() call. Slightly more expensive, but the calls to FBOAuth are only called on admin pages and when logging in through Facebook, so I don't think the overhead should be a concern.
Comment #8
quicksketchSorry patch intended for #7 attached.
Comment #9
milesw commented@quicksketch: Patch in #8 works great for me.
Jumping the gun a little here, but I posted some API documentation for this hook: #1554182: Document alter hooks in API documentation. I used Geofield as a simple example. AddressField is a bit more complex due to the need for geocoding.
I wonder if there's a good place for madmatter23's example code. It's quite useful.
Comment #10
BenK commentedGreat to find this thread. Patch #8 looks good to me as well.
Comment #11
quicksketchGreat, committed #8 to the project.
Comment #12
grasmash commentedAs promised, here's a quick blog post outlining how you can use this new feature to map facebook information to addressfields:
http://grasmash.com/node/68
Comment #14
mototribe commentedFYI, http://stackoverflow.com/questions/4609781/facebook-api-can-i-get-detail... shows how to get the country from a location id - no geocoding necessary.
Comment #14.0
mototribe commentedfixed typo