You are here

function entityreference_autocreate_feeds_set_target in Entityreference Autocreate 7

Entity reference callback for mapping.

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 $source: A FeedsSource object.

StdClass $entity: The entity to map to.

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

array $value: The value to map. MUST be an array.

array $mapping: Array of mapping settings for current value.

bool $input_format: TRUE if an input format should be applied.

1 string reference to 'entityreference_autocreate_feeds_set_target'
entityreference_autocreate_feeds_processor_targets_alter in ./entityreference_autocreate.feeds.inc
Announce that we know how to set data on an entityreference field.

File

./entityreference_autocreate.feeds.inc, line 74
Feeds mapping implementation for the Entity reference module.

Code

function entityreference_autocreate_feeds_set_target($source, $entity, $target, $value, $mapping = array(), $input_format = FALSE) {

  // Don't do anything if we weren't given any data.
  if (empty($value)) {
    return;
  }

  // Assume that the passed in value could really be any number of values.
  if (is_array($value)) {
    $values = $value;
  }
  else {
    $values = array(
      $value,
    );
  }

  // Get some useful field information.
  $field_info = field_info_field($target);

  // Set the language of the field depending on the mapping.
  $language = isset($mapping['language']) ? $mapping['language'] : LANGUAGE_NONE;

  // Iterate over all values.
  $iterator = 0;

  // Re-use the existing field data if it's set.
  $field = isset($entity->{$target}) ? $entity->{$target} : array();
  foreach ($values as $value) {

    // Only process if this value was set for this instance.
    if ($value) {

      // Use the same processes that autocomplete would,
      // scan for a nid inside ()
      // or look for an exact text mach
      // or if not found, make one up.
      // Abort if more than one exact match was returned.
      //
      // If the widget is tags style, the title string may need exploding,
      // and we may create more than one entry.
      // ASSUME this may need to happen each time, as it's really hard to tell
      // if it really should. tags is a widget setting and we know nothing
      // about the widget.
      //
      $titles = drupal_explode_tags($value);

      // Will at least be an array of 1.
      foreach ($titles as $title) {
        if ($target_id = entityreference_autocreate_get_entity_by_title($field_info, $title)) {
          $field[$language][$iterator]['target_id'] = $target_id;
          $iterator++;
        }
        else {

          // Failed to create the target for some reason.
        }
      }
    }

    // Break out of the loop if this field is single-valued.
    if ($field_info['cardinality'] == 1) {
      break;
    }
  }

  // Add the field to the entity definition.
  $entity->{$target} = $field;
}