You are here

function location_feeds_set_country in Location Feeds 7

Same name and namespace in other branches
  1. 6 location_feeds.module \location_feeds_set_country()

Set the country and attempt to support non-iso country imports

Parameters

$source: Field mapper source settings.

$entity: Either a user or node object dpending on where this is called

$target: When targeting sub arrays the '][' is used to drill down. Note that this implementation is lazy ... we assume just depth=2

$value:

Return value

object

1 string reference to 'location_feeds_set_country'
_location_feeds_fill_targets in ./location_feeds.module
Helper function to add target fields

File

./location_feeds.module, line 215
This module provides location mappers to feed importers.

Code

function location_feeds_set_country($source, $entity, $target, $value) {
  module_load_include('inc', 'location', 'location');
  list($field_name, $sub_field) = explode(':', $target);
  static $iso_list = array();
  if (!is_array($value)) {
    $value = array(
      $value,
    );
  }

  // Check if this is a cck field since they're added to the node
  // differently.
  $cck = preg_match('/^field_.+/', $target);
  foreach ($value as $i => $val) {
    $val = trim($val);

    // if the country is in iso format or close to it, store
    // it, else try to figure out the iso format
    $isoval = $val;
    if (location_standardize_country_code($isoval)) {
      if ($cck) {
        if (!isset($entity->{$field_name}['und'][$i]['location_settings'])) {
          $entity->{$field_name}['und'][$i]['location_settings'] = location_feeds_get_field_info($field_name);
        }
        $entity->{$field_name}['und'][$i][$sub_field] = $isoval;
      }
      else {
        $entity->{$field_name}[$i][$sub_field] = $isoval;
      }
    }
    else {

      // only load this once...
      if (sizeof($iso_list) == 0) {
        $iso_list = location_get_iso3166_list();
      }
      $keys = array_keys($iso_list, $val);
      if (isset($keys[0])) {
        if ($cck) {
          if (!isset($entity->{$field_name}['und'][$i]['location_settings'])) {
            $entity->{$field_name}['und'][$i]['location_settings'] = location_feeds_get_field_info($field_name);
          }
          $entity->{$field_name}['und'][$i][$sub_field] = $keys[0];
        }
        else {
          $entity->{$field_name}[$i][$sub_field] = $keys[0];
        }
      }
    }
  }
  return $entity;
}