You are here

function nodereference_feeds_set_target in Feeds 6

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.

Parameters

stdClass $node: The node to to be created or updated.

str $target: The name of the field on the node to be mapped.

str|array $value: The value(s) of the source feed item element to be mapped.

1 string reference to 'nodereference_feeds_set_target'
nodereference_feeds_node_processor_targets_alter in mappers/nodereference.inc
Implementation of hook_feeds_node_processor_targets_alter().

File

mappers/nodereference.inc, line 66
Implementation of Feeds API for mapping nodereference.module fields (CCK).

Code

function nodereference_feeds_set_target($node, $target, $value) {

  // Determine whether we are matching against title, nid, URL, or GUID.
  list($target, $match_key) = explode(':', $target, 2);

  // Load field definition.
  $field_info = content_fields($target, $node->type);

  // Allow for multiple-value fields.
  $value = is_array($value) ? $value : array(
    $value,
  );

  // Allow importing to the same target with multiple mappers.
  $field = isset($node->{$target}) ? $node->{$target} : array();

  // Match values against nodes and add to field.
  foreach ($value as $v) {
    $nids = array();
    $v = trim($v);
    switch ($match_key) {
      case 'url':
      case 'guid':

        // Lookup node ID by Feeds unique value.
        $result = db_query("SELECT nid FROM {feeds_node_item} WHERE %s = '%s'", $match_key, $v);

        // Since GUID and URL are only guaranteed to be unique per feed,
        // multiple nids from different feeds may result.
        while ($row = db_fetch_array($result)) {
          $nids[] = $row['nid'];
        }

        // Ensure nids are valid node ids for this field.
        $nids = !empty($nids) ? array_keys(_nodereference_potential_references($field_info, '', NULL, $nids)) : array();
        break;
      case 'title':

        // Validate title.
        if (is_string($v) && $v !== '' || is_numeric($v)) {

          // Lookup potential exact matches for the value.
          $nids = array_keys(_nodereference_potential_references($field_info, $v, 'equals', array()));
        }
        break;
      case 'nid':

        // Ensure nid is a valid node id for this field.
        $nids = array_keys(_nodereference_potential_references($field_info, '', NULL, array(
          $v,
        )));
        break;
    }
    if (empty($nids)) {

      // Alert if no matches were found.
      drupal_set_message(t("'%value' does not match a valid node %key for the '%field' field.", array(
        '%value' => $v,
        '%key' => $match_key,
        '%field' => $target,
      )));
    }
    else {

      // Add the reference (ignoring duplicates).
      foreach ($nids as $nid) {
        $field[] = array(
          'nid' => $nid,
        );
      }
    }
  }
  $node->{$target} = $field;
}