The docs say:

 * @return int
 *   The existing entity id, or 0 if not found.

but the implementation in the tests returns NULL when nothing is found:

/**
 * Callback for unique_callbacks for test_target mapper.
 *
 * @see feeds_tests_feeds_processor_targets_alter()
 */
function feeds_tests_mapper_unique(FeedsSource $source, $entity_type, $bundle, $target, array $values) {
  $query = new EntityFieldQuery();
  $result = $query
    ->entityCondition('entity_type', $entity_type)
    ->entityCondition('bundle', $bundle)
    ->fieldCondition('field_alpha', 'value', $values)
    ->execute();

  if (!empty($result[$entity_type])) {
    return key($result[$entity_type]);
  }
}

I would suggest the documentation is amended. It's better DX to return nothing when nothing is found than have to explicitly return a value.

Comments

megachriz’s picture

You are right. The "unique" callback doesn't have to return 0 if the entity is not found. Returning NULL or no value is good as well. I think these docs were confused with FeedsProcessor::existingEntityId(), which does return 0 if no existing entity is found.

joachim’s picture

Status: Active » Needs review
StatusFileSize
new468 bytes

Thanks for filling in the info!

Here's a patch.

megachriz’s picture

Status: Needs review » Reviewed & tested by the community

In an earlier patch from #661606: Support unique targets in mappers (which added the unique callback example) (patch at #175), the second part of the @return doc was as such:
Or NULL if no existing entity is found.

But I think your version is good as well.

  • twistor committed 006234f on 7.x-2.x authored by joachim
    Issue #2397219 by joachim, MegaChriz: docs for my_module_mapper_unique...
twistor’s picture

Status: Reviewed & tested by the community » Fixed

Thanks. Added suggestion from #3.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.