You are here

function countries_feeds_processor_targets_alter_callback in Countries 7

Callback defined via countries_feeds_processor_targets_alter().

1 string reference to 'countries_feeds_processor_targets_alter_callback'
countries_feeds_processor_targets_alter in ./countries.module
Implements hook_feeds_processor_targets_alter().

File

./countries.module, line 692

Code

function countries_feeds_processor_targets_alter_callback($source, $entity, $target, $values) {
  $values = (array) $values;

  // Find the field name and country property keys.
  list($entity_type, $bundle_name, $field_name, $property) = explode(':', $target, 4);
  $instance = field_read_instance($entity_type, $field_name, $bundle_name);
  $settings = $instance['widget']['settings'];
  $continents = array_filter((array) $settings['continents']);
  $field = field_read_field($field_name);
  $entity->{$field_name} = isset($entity->{$field_name}) ? $entity->{$field_name} : array();
  foreach ($values as $value) {
    if ($country = countries_country_lookup($value, $property)) {

      // Check instance settings
      if ($settings['enabled'] == COUNTRIES_ENABLED && !$country->enabled) {
        continue;
      }
      elseif ($settings['enabled'] == COUNTRIES_DISABLED && $country->enabled) {
        continue;
      }
      if (!empty($continents)) {
        if (!array_key_exists($country->continent, $continents)) {
          continue;
        }
      }
      $entity->{$field_name}[$entity->language][]['iso2'] = $country->iso2;
    }
    if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && count($entity->{$field_name}[$entity->language]) >= $field['cardinality']) {
      break;
    }
  }
  return $entity;
}