You are here

function phone_feeds_set_target in Phone 7.2

Same name and namespace in other branches
  1. 7 phone.feeds.inc \phone_feeds_set_target()

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.

1 string reference to 'phone_feeds_set_target'
phone_feeds_processor_targets_alter in ./phone.feeds.inc
Implements hook_feeds_processor_targets_alter().

File

./phone.feeds.inc, line 64
Implements Feeds support for Phone fields.

Code

function phone_feeds_set_target($source, $entity, $target, $value) {
  if (empty($value)) {
    return;
  }

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

  // Iterate over all values.
  list($field_name, $column) = explode(':', $target);
  if (strpos($target, ':') === FALSE) {
    $field_name = $target;
    $column = NULL;
  }
  else {
    list($field_name, $column) = explode(':', $target);
  }
  $info = field_info_field($field_name);
  $field = isset($entity->{$field_name}) ? $entity->{$field_name} : array();
  $delta = 0;
  foreach ($value as $v) {
    if (is_object($v) && $v instanceof FeedsElement) {
      $v = $v
        ->getValue();
    }
    if (is_scalar($v)) {
      if (!isset($column)) {

        // Convert whole number into components.
        $item = _phone_migrate_phone_number($v);
        foreach ($item as $column => $sub_value) {
          $field[LANGUAGE_NONE][$delta][$column] = $sub_value;
        }
      }
      else {
        if ($column == 'countrycode') {

          // Note: allowing non-allowed countries through here, to avoid
          // data loss.  Any attempt to edit the field will force user
          // to change the data
          // @todo: is this what should be done for feeds?  Should there
          // be a setting allowing user to determine whether to apply
          // strict standards?
          $v = strtoupper($v);
          $country_info = phone_countries($v);
          if (empty($country_info)) {
            $v = NULL;
          }
        }
        elseif ($column == 'number' || $column == 'extension') {

          // Strip all non-numbers from number/extension.
          // @todo: Does this need to be changed to allow vanity numbers?
          $v = preg_replace('/[^\\d]/', '', $v);
        }
        elseif ($column == 'numbertype') {

          // @todo: needs to check that numbertype is an hcard type,
          // convert to match the case being used in module, and
          // combine multiple values using appropriate separator
        }
        $field[LANGUAGE_NONE][$delta][$column] = $v;
      }
    }
    $delta++;
    if ($info['cardinality'] != FIELD_CARDINALITY_UNLIMITED && $delta >= $info['cardinality']) {
      break;
    }
  }
  $entity->{$field_name} = $field;
}