Skip to content

Instantly share code, notes, and snippets.

@evandonovan
Created June 30, 2010 20:04
Show Gist options
  • Save evandonovan/459143 to your computer and use it in GitHub Desktop.
Save evandonovan/459143 to your computer and use it in GitHub Desktop.
Feeds mapping for location_cck.module
<?php
// $Id$
/**
* @file
* On behalf implementation of Feeds mapping API for location_cck.module (Location CCK).
*/
/**
* Implementation of hook_feeds_node_processor_targets_alter().
*
* @see FeedsNodeProcessor::getMappingTargets().
*/
function location_feeds_node_processor_targets_alter(&$targets, $content_type) {
$info = content_types($content_type);
if (isset($info['fields']) && count($info['fields'])) {
foreach ($info['fields'] as $field_name => $field) {
if (in_array($field['type'], array('location'))) {
$name = isset($field['widget']['label']) ? $field['widget']['label'] : $field_name;
$targets[$field_name .':latitude'] = array(
'name' => $name .': Latitude',
'callback' => 'location_feeds_set_target',
'description' => t('The latitude for the @name field. Use Latitude as the source - both Latitude and Longitude are required.', array('@name' => $name)),
);
$targets[$field_name .':longitude'] = array(
'name' => $name .': Longitude',
'callback' => 'location_feeds_set_target',
'description' => t('The longitude for the @name field. Use Longitude as the source - both Latitude and Longitude are required.', array('@name' => $name)),
);
}
}
}
}
/**
* Callback for mapping. Here is where the actual mapping happens.
*
* When the callback is invoked, $target contains the name of the field the
* user has decided to map to and $value contains the value of the feed item
* element the user has picked as a source.
*/
function location_feeds_set_target(&$node, $target, $value) {
list($field_name, $sub_field) = split(':', $target);
$field = isset($node->$field_name) ? $node->$field_name : array();
// Handle multiple value fields.
if (is_array($value)) {
$i = 0;
foreach ($value as $v) {
if (!is_array($v) && !is_object($v)) {
if ($sub_field == 'latitude') {
$field[$i]['latitude'] = $v;
}
elseif ($sub_field == 'longitude') {
$field[$i]['longitude'] = $v;
}
}
$i++;
}
}
else {
if ($sub_field == 'latitude') {
$field[0]['latitude'] = $v;
}
elseif ($sub_field == 'longitude') {
$field[0]['longitude'] = $v;
}
}
$node->$field_name = $field;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment