You are here

function data_entity_feed_unique_callback in Data 7

Feeds unique callback for Data table field targets.

Parameters

FeedsSource $source: The Feed source.

string $entity_type: Entity type for the entity to be processed.

string $bundle: Bundle name for the entity to be processed.

string $target: A string identifying the unique target on the entity.

array $values: The unique values to be checked.

Return value

int The existing entity id, or NULL if nothing is found.

See also

data_entity_feeds_processor_targets_alter().

FeedsProcessor::existingEntityId()

1 string reference to 'data_entity_feed_unique_callback'
data_entity_feeds_processor_targets_alter in data_entity/data_entity.module
Implements hook_feeds_processor_targets_alter().

File

data_entity/data_entity.module, line 286
data_entity.module TODO: Enter file description here.

Code

function data_entity_feed_unique_callback(FeedsSource $source, $entity_type, $bundle, $target, $values) {

  // Get the information about this entity.
  $entity_info = entity_get_info($entity_type);

  // Extract the entity ID key.
  $entity_id_key = $entity_info['entity keys']['id'];

  // Attempt to load the table.
  if ($table = data_get_table($entity_info['base table'])) {

    // Get the single value out of the array. Not sure why it has to be an array
    // but as we deal in database columns, we know it's always a single value.
    $value = array_pop($values);

    // Check if this value already exists in this table.
    if ($record = $table
      ->handler()
      ->load(array(
      $target => $value,
    ))) {

      // Use the first record.
      $record = reset($record);

      // Determine the primary key value.
      if (isset($record[$entity_id_key])) {
        return $record[$entity_id_key];
      }
    }
  }

  // Nothing found.
}