You are here

function references_feeds_set_target in References 7.2

Callback for mapping both node reference and user_reference fields.

Implementation of hook_feeds_set_target().

Parameters

object $source: A FeedsSource object.

object $entity: The entity to map to.

string $target: The target key on $entity to map to.

mixed $value: The value to map. Can be an array or a string.

2 string references to 'references_feeds_set_target'
node_reference_feeds_processor_targets_alter in ./references.feeds.inc
Implements hook_feeds_processor_targets_alter() for node_reference fields.
user_reference_feeds_processor_targets_alter in ./references.feeds.inc
Implements hook_feeds_processor_targets_alter() for user_reference fields.

File

./references.feeds.inc, line 215
References Feed File.

Code

function references_feeds_set_target($source, $entity, $target, $value) {
  if (empty($value) || empty($value[key($value)])) {
    return;
  }

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

  // Determine the field we are matching against, and whether duplicates are
  // allowed.
  $target_info = explode(':', $target, 3);
  if (count($target_info) == 3) {
    list($target, $match_key, $duplicates) = $target_info;
  }
  else {
    list($target, $match_key) = $target_info;
  }

  // Load field definition.
  $info = field_info_field($target);

  // Parameters to handle differences between node references and user
  // references.
  if ($info['type'] == 'user_reference') {
    $idname = 'uid';
    $typename = 'user';
    $validate_function = 'user_reference_potential_references';
  }
  else {
    $idname = 'nid';
    $typename = 'node';
    $validate_function = 'node_reference_potential_references';
  }
  $field = isset($entity->{$target}) ? $entity->{$target} : array();
  if (!isset($field[LANGUAGE_NONE])) {
    $field[LANGUAGE_NONE] = array();
  }

  // Match values against nodes and add to field.
  foreach ($value as $v) {

    // Create options.
    $options = array(
      'string' => $v,
      'match' => 'equals',
      'ids' => array(),
      'limit' => 1,
    );
    switch ($match_key) {
      case 'title':
      case 'name':

        // Validate node title or user name.
        if (is_string($options['string']) && $options['string'] != '' || is_numeric($options['string'])) {

          // Lookup potential exact matches for the value (limit to one result).
          $matches = $validate_function($info, $options);

          // Use the first element of the potential matches.
          $options['ids'] = key($matches);
        }

        // Alert if no match is found.
        if (empty($options['ids'])) {
          drupal_set_message(t('%title does not match an existing @typename', array(
            '%title' => $options['string'],
            '@typename' => $typename,
          )));
        }
        break;
      case 'nid':
      case 'uid':

        // Make sure it is a positive integer.
        if ((is_int($options['string']) || ctype_digit($options['string'])) && $options['string'] > 0 && $options['string'] !== '') {

          // Make sure it is a valid node id or user id for this field.
          $matches = $validate_function($info, array(
            $options['string'],
          ));
          foreach ($matches as $k => $v) {
            if ($options['string'] == $k) {
              $options['ids'] = $k;
            }
          }
        }

        // Alert if no match is found.
        if (empty($options['ids'])) {
          drupal_set_message(t('%id is not a valid @typename id for this field.', array(
            '%id' => $options['string'],
            '@typename' => $typename,
          )));
        }
        break;
      case 'guid':
      case 'url':

        // Get the value from table feeds-item.
        $result = db_query('SELECT f.entity_id FROM {feeds_item} f WHERE f.' . $match_key . ' = :v', array(
          ':v' => $v,
        ));
        $options['ids'] = $result
          ->fetchField();

        // Alert if no match is found.
        if (empty($options['ids'])) {
          drupal_set_message(t('%id is not a valid @typename id for this field.', array(
            '%id' => $v,
            '@typename' => $typename,
          )));
        }
        break;
    }
    if (!empty($options['ids'])) {
      $reference = array(
        $idname => $options['ids'],
      );
      if (!empty($duplicates)) {

        // Add the reference, ignoring duplicates.
        $field[LANGUAGE_NONE][] = $reference;
      }
      elseif (!in_array($reference, $field[LANGUAGE_NONE])) {

        // Add the reference only if it doesn't already exist.
        $field[LANGUAGE_NONE][] = $reference;
      }
      if ($info['cardinality'] == 1) {
        break;
      }
    }
  }
  $entity->{$target} = $field;
}