You are here

function _countries_feeds_set_target in Countries 8

Same name and namespace in other branches
  1. 7.2 countries.feeds.inc \_countries_feeds_set_target()

Callback defined via countries_feeds_processor_targets_alter().

1 string reference to '_countries_feeds_set_target'
countries_feeds_processor_targets_alter in ./countries.feeds.inc
Implements hook_feeds_processor_targets_alter().

File

./countries.feeds.inc, line 38
Feeds processor hooks for importing Countries fields using the Feeds module.

Code

function _countries_feeds_set_target($source, $entity, $target, $values) {
  if (empty($values)) {
    return;
  }

  // Handle non-multiple values.
  if (!is_array($values)) {
    $values = array(
      $values,
    );
  }

  // Find the field name and country property keys.
  list($entity_type, $bundle_name, $field_name, $property) = explode(':', $target, 4);
  $field = field_read_field($field_name);
  $settings = $field['settings'];
  $continents = array_filter((array) $settings['continents']);

  // Iterate over all values.
  $i = 0;
  $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}[LANGUAGE_NONE][$i]['iso2'] = $country->iso2;
      $i++;
    }
    if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && $i >= $field['cardinality']) {
      break;
    }
  }
  return $entity;
}