D6 - Location all fields mappings
gionnibgud - January 23, 2009 - 20:20
| Project: | Feed Element Mapper |
| Version: | 6.x-1.x-dev |
| Component: | Code |
| Category: | bug report |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Description
I'm posting the mapper inc for location modified to include all location fields.
I'm note sure if [province_name] and [country_name] are necessary or of any use since it looks like locations module fills them up automagically ;) from province and country codes fields.
The code needs some testing for sure.
| Attachment | Size |
|---|---|
| feedapi_mapper_location.inc_.txt | 2.31 KB |

#1
Is this a duplicate? #333519: Feedapi_mapper_location country, province, city, street and postal_code fields
#2
I first posted the array in #333519 because it can be useful for 5.x and then created this issue for 6.x since i didn't test it in d5. We can merge if you want.
#3
Change title?
#4
This patch works for normal Locations gionnibgud (adding your code as a patch against current dev).
But do you have any ideas on adding mapping functionality for Location CCK fields?
#5
Hi thanks for the patch.
I've never worked with Location cck fields but mapping in this module of CCK is quite straightforward.
The codebase syntax is more or less similar for all mapper so most of the job is understanding how to fill the $node->field_field_name variable. You can do this by placing a print_r($node->field_field_name) in the node template and check the values that the field is expecting you to fill in.
#6
I tried this patch, it works great for all locations fields (not cck). The only problem I encountered is more about the Country. I tried to map to the "Country" and the "Country name" and none of them would actually save the appropriate value, in this case, "Switzerland"...
ANyone else has this issue while trying to map Data to location module using the mapper ?
BTW, I'm using the csv parser to include data, so maybe that is something to consider as well??
Thanks a lot!
Patchak
#7
@Patchak: Which format were you using in your country field? It's supposed to be 'ch' in your case. Is it set in the as default country?
I also think that country name field is not necessary if the country (id: ex. ch) field is present.
#8
@gionnibgud: I was able to work with other contrib cck modules pretty easily, but so far I haven't had much luck with the location cck fields. I'd be happy to work on this some more, but - would you mind taking a look? Maybe I just got tired over the weekend and was missing something obvious :p
#9
Ah ok I used the actual name of the country. I wil try with CH as the Country...
Thanks!
#10
@scottrigby: Ok i took a look. It looks tricky since location cck is just a reference to a location stored in location's table.
I printed out the form array to see what kind of input is expecting and as you can see it's the same structure of a regular location field.
Array
(
[#theme] => content_multiple_values
[#title] => Test (key=field_via, weight=32)
[#required] => 0
[#description] =>
[0] => Array
(
[#type] => location_element
[#title] => Test (key=0, weight=0)
[#description] =>
[#required] =>
[#default_value] => Array
(
[lid] =>
[name] =>
[street] =>
[additional] =>
[city] =>
[province] =>
[postal_code] =>
[country] =>
[latitude] =>
[longitude] =>
[source] =>
[is_primary] =>
[province_name] =>
[country_name] =>
)
[#weight] => 0
[#delta] => 0
[#columns] => Array
(
[0] => lid
)
[#field_name] => field_via
[#type_name] => page
[#post] => Array
(
)
[#programmed] =>
[#tree] => 1
[#parents] => Array
(
[0] => field_via
[1] => 0
)
[#array_parents] => Array
(
[0] => field_via
[1] => 0
)
[#processed] => 1
[#attributes] => Array
(
[class] => location
)
[#input] => 1
[#process] => Array
(
[0] => _location_expand_location
)
[#validate] => Array
(
[location_element_validate] => Array
(
)
)
[#name] => field_via[0]
[#id] => edit-field-via-0
[#value] => Array
(
[lid] =>
[name] =>
[street] =>
[additional] =>
[city] =>
[province] =>
[postal_code] =>
[country] =>
[latitude] =>
[longitude] =>
[source] =>
[is_primary] => 0
[province_name] =>
[country_name] =>
)
[location_settings] => Array [.... and more, not relevant]
So this is what i think might work, treating location cck separately from location and create a new .inc. It's the same script as location mapper except that I map the fields as they were normal cck fields instead of location fields and fill $field_name; ?>instead of
<?php$node->locations
?>
I did not have any time to test it please review it before trying it. I REALLY didn't test it! ;) so be careful. :)
I only mapped Lat Long but it's just a matter of adding subfields.
<?php
function location_cck_feedapi_mapper($op, $node, $field_name, $feed_element = array(), $sub_field = '') {
if (strpos($field_name, 'field_') === 0) {
if ($op == 'describe') {
if (feedapi_mapper_content_is_cck_type($field_name, array('location_element'))) { // treat it as a normal cck field
return t('Maps pairs of geographic coordinates to the location cck fields of a node.');
}
}
else if ($op == 'list') {
$sub_fields['latitude'] = 'Latitude';
$sub_fields['longitude'] = 'Longitude';
return $sub_fields;
}
else if ($op == 'map') {
if (is_array($feed_element)) {
foreach ($feed_element as $i => $val) {
$node->$field_name[$i][$sub_field] = $val;
}
} else {
$node->$field_name[0][$sub_field] = $feed_element;
}
return $node;
}
}
else {
return FALSE;
}
}
?>
#11
Hi,
I have feedapi_mapper working on D6 with all fields (not using CCK)
I only had to change location.module, because country is not in ISO values, but like "Holland, Austria" in my feeds.
I also had to alter the databasefield location.country.
May be it would be possible to have an option in the mapper to convert the feedvalues to the right values related to location.module. Location module checks for ISO values, and most feeds do not have the ISO values as countries.
So adding something like this in pseudocode:
if feed-field = America, location-country = USIf feed-field = Holland, location-country = NL
May be countries_api can be used.. http://drupal.org/node/217070 ?
or http://drupal.org/project/geonames ?
I attach my working feedapi_mapper for location to add to this discussion.
Greetings,
Martijn
www.reizen-naar.nl
#12
@Summit:
There is this function from location module
<?phplocation_get_iso3166_list();
?>
Maybe something like this would work:
First use the correct labels for location module:
<?php
$sub_fields['country'] = 'Country ISO';
$sub_fields['country_name'] = 'Country Name';
?>
Then during the actual mapping of the country name field we convert to ISO and fill in the country code field. There's no need to fill the country name field since location do that for you if you set the country (ISO) field.
<?php
if ($sub_field == 'country_name') {
$countries = location_get_iso3166_list(); // load the 'iso => country name' array
$country_code = array_search($feed_element,$countries); // return the key for our country name which is our iso code
$feed_element = $country_code; // We're going to pass an iso code...
$sub_field == 'country'; //...but we want to do it on the right location field so it's 'country' not 'country_name'
}
$node->locations[0][$sub_field] = $feed_element;
?>
#13
@gionnibgud,
Thanks for thinking this true.. one problem I still see.
If the feed has the country_name not explicit as it should be to use
array_search($feed_element,$countries);because it is in another language or spelled differently, like "The united states of America" or something like this.
How can we first convert these sort of country-spelling to correct spelling using the iso function?
Thanks a lot in advance for your reply!
#14
@Summit:
Translation could be handled, the location_get_iso3166_list function returns a tranlated ( 'us' => t('United States'))....
Different spelling well, i don't know, maybe reg exp or geocoding using google. But after all is geographical data, it has to be a little accurate, doesn't it? What kind of feed is that! No geo data?
#15
@gionnibgud,
they are called productfeeds, and every productfeed has its own geographical words, and mostly no lat/lon.
To get the info correctly in the location field, some different spelling would be great. Do you have experience in this reg. exp. stuff?
greetings,
Martijn
#16
Hey all, it seems this is now broken with the new locations rc-1 that was released a couple days ago...I tested the new locations version and the mapper did not seemed to work at all...
Anyone can confirm this?
Patchak
#17
So, has anyone successfully mapped country names or codes in a feed to the Location (not CCK) country field? I've tried adding the subfields to the location mapper with no luck. I feel I'm missing something that should be blatantly easy.
#18
To clarify: this patch is failing for me; D6, Location 3.0, FeedAPI_mapper 1.0-beta4, feedapi 1.6. The locations fields (country, etc.) are visible in feedapi_mapper_location.inc , but on the field mapping page, only lat and long show up. Are there any other tricks to getting the rest of the location fields visible? Do I need to hack parser_simplepie.module? Has anyone tried reverse geocoding from lat/long (Bdragon's tentative suggestion)?
#19
To further clarify: I'm an idiot.
patching feedapi_mapper/mappers/feedapi_mapper_location.inc instead of feedapi_mapper/feedapi_mapper_location.inc works wonders.
However -- I'm only successfully getting Lat/Long OR one other piece of location imported (e.g. if I import a street, a state, lat/ long and country, the only thing that "sticks" seems to be country. If I only import lat/long, both fields import fine.
#20
Nudge (even patching correctly has not actually solved this problem; importing non-Lat/Long data removes the lat/long data)
#21
Subscribing still issues..greetings, Martijn
#22
I've tested it and can confirm that it does not work with the new locations RC-1. It looks like they've dropped out some old code which the location mapper references - specifically the variable "location_maxnum_" does not exist in the location.module now.
#23
I think it is better than setting this to a bugreport, while it worked and doesn't work on RC1 anymore.
Location_mapper not working after security update.
Thanks for going into this in advance!
Greetings,
Martijn
#24
Hi,
Location_mapper is still not showing any more since beta5, since the security update...
Anyone else has this same experience? I really like location_mapper, so please assist.
greetings,
Martijn
#25
Hi, from beta9 and further location is only supported through cck see: http://drupal.org/node/416052#comment-1636334.
So we have to do the code differently I think.
greetings, Martijn
#26
Hi,
With newest location-cck all subfield support will be using following code in feedapi_mapper_location.inc. The only thing I added to the original code is:
, 'name' => t('Location name'),'street' => t('Street location'), 'additional' => t('Additional'),
'city' => t('City'), 'province' => t('State/Province'),
'postal_code' => t('Postal code'), 'country' => t('Country')
The whole code will be:
<?php
// $Id: feedapi_mapper_location.inc,v 1.1.2.3 2009/04/20 13:31:04 alexb Exp $
/**
* On behalf implementation of hook_feedapi_mapper() for location module.
*
* @see hook_feedapi_mapper()
*/
function location_feedapi_mapper($op, $node, $feed_element = array(), $field_name = '', $sub_field = '') {
if ($op == 'describe') {
return t('Maps pairs of geographic coordinates to the location of a node.');
}
else if ($op == 'list') {
$sub_fields = array('latitude' => t('Latitude'), 'longitude' => t('Longitude'), 'name' => t('Location name'),
'street' => t('Street location'), 'additional' => t('Additional'),
'city' => t('City'), 'province' => t('State/Province'),
'postal_code' => t('Postal code'), 'country' => t('Country') );
$info = content_types($node->type);
$fields = array();
if (@count($info['fields'])) {
foreach ($info['fields'] as $field_name => $field) {
if (in_array($field['type'], array('location'))) {
$fields[$field_name] = $sub_fields;
}
}
}
if (count($fields)) {
return $fields;
}
return FALSE;
}
else if ($op == 'map') {
if (is_array($feed_element)) {
foreach ($feed_element as $i => $val) {
$node->{$field_name}[$i][$sub_field] = $val;
}
}
return $node;
}
}
?>
EDIT:
Looking to integrate something from above thread into this, like:
<?php
if ($sub_field == 'country_name') {
$countries = location_get_iso3166_list(); // load the 'iso => country name' array
==> Some code still to change the countryname in the feed, to the right countryname as iso subscribes..
==> Any help appreciated with this!!!
$country_code = array_search($feed_element,$countries); // return the key for our country name which is our iso code
$feed_element = $country_code; // We're going to pass an iso code...
$sub_field == 'country'; //...but we want to do it on the right location field so it's 'country' not 'country_name'
}
$node->locations[0][$sub_field] = $feed_element;
?>
==> Some code still to change the countryname in the feed, to the right countryname as iso subscribes..
==> Any help appreciated with this!!!
something with: $country_code = $countries[$country_name]; where first country_name is being converted from a array of all posibilities in the feeds possible.
Greetings, Martijn
#27
What if you want the venue name, street, etc, and the coordinates? Does the above give you either and not both?
Chris
Use case- importing event information. The view name and human readable address is always a good thing to have.
Update:
Just tried the new location.inc file above. Great file btw.
ISSUE 1:
The addresses map (location name, street, etc) show up if I specify them
If I also specify long/lat in addition to address fields, longitude and latitude are not mapped
If I just map longitude and latitude without the address fields, they still are not mapped.
SOLUTION TO #1:
I mapped country field which is a drop down in my cck node field. So none of the location parameters worked as they should. As soon as I removed country, then all information (address and long/lat) were mapped.
ISSUE 2:
I get this warning. The file is not present. I am not sure why it asks for location.un.inc.
warning: include_once(sites/all/modules/location/supported/location.un.inc) [function.include-once]: failed to open stream: No such file or directory in /drupal/sites/all/modules/location/location.inc on line 482.
#28
Hi follow country-specific location_cck_mapper here: http://drupal.org/node/489680
Made a new issue, while I thought this has nothing to do anymore with mapping all fields.
greetings,
Martijn
#29
Ok, I figured out why you're getting the error about not finding location.un.inc, as I was having the same problem. It comes from bad data in the country field of the location table. In my case, I had some bad custom code that wrote the country name instead of the country abbreviation into location.country. In this case, the country was 'United States'. Once I changed 'Un' to 'us' in the database this error went away. My guess is that you're having the same problem. Perhaps someone will fix the location module to better handle a case like this.