Problem/Motivation

The latest dev of Feeds (August 27, 2014) supports unique target in mappers (now that patch #195 of #661606: Support unique targets in mappers has been committed), but it would need another module to handle the unique field value validation.

The Field validation module already has integration code for this feature (see field_validation.feeds.inc), but that is based on an older patch from #661606: Support unique targets in mappers. Specifically, the parameters for the unique callback were changed to line them up with similar callback functions in Feeds. As such, the implementation in Field validation is outdated.

Proposed resolution

Update the Field validation's implementation.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

scottrigby’s picture

Title: Feeds integration » Feeds integration (Field validation)

Changing issue title for clarity

scottrigby’s picture

Status: Active » Needs review

There is a linked patch ready for review

g089h515r806’s picture

Unique target in mappers is very usefull when import/update content using views.

agileadam’s picture

The patches in #661606-87: Support unique targets in mappers worked great for me. I see there are now later version of those two patches. I'm going to do some comparing to see what's different.

Once this is all sorted out, someone should update http://drupal.org/node/1307732 to add some documentation about using Field Validation to enable the "Unique" option within feeds.

g089h515r806’s picture

FileSize
985 bytes

Here is the code that works with patch "feeds-unique-target-661606-115.patch",
unzip it and copy "field_validation.feeds.inc" file into your field validation directory.

g089h515r806’s picture

Status: Needs review » Fixed

I have commit the code of #5.
I also need this feature.
Need to apply patch http://drupal.org/node/661606#comment-6481214 to make it work.

Status: Fixed » Closed (fixed)

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

Jason Dean’s picture

Hi

I was using field_validation.feeds.inc from #5 and the Feeds patch in #6 and everything was working fine.

Then I upgraded Feeds to latest 7.x-2.0-alpha7 and reapplied the patch in #6

Now in the importer mappings, it looks like the 'unique target' column has been changed and I can no longer select my validated field :(

g089h515r806’s picture

Status: Closed (fixed) » Active

For feeds alpha7, we need to increase the weight of field validation in system table.
I have committed some code to updte the weight value in install file.

zeezhao’s picture

I tried latest dev version and got this error while trying to edit a field previously set up:

Parse error: syntax error, unexpected T_STATIC, expecting ')' in ....\modules\field_validation\field_validation_extras\plugins\validator\field_validation_date_validator.inc on line 24
g089h515r806’s picture

field_validation_date_validator is a new validator, has not been not full tested.
But i test it on a text field, it works.

2pha’s picture

Can someone give me a point in the right direction on how to get this field validation working with Feeds 7.x-2.0-alpha7.
I have been trying to get unique checks in feeds all day. I have been scouring the issues related to this in the Feeds module, but most of the issues are getting a little out of control.

I have a feeds imported (from csv) to import but I can't use the GUID because some content has been added before through the interface. So I want to use one of the fields as unique.
I have installed the fields validation module and my validation rule works fine when creating content (content is not created if field is not unique within the bundle).
Now I need it to work with feeds. After installing this module, should I be able to select the field (the one with the validation rule) as unique in the feeds importer? Because I can't.

2pha’s picture

Ok, I've worked out why I was not seeing a make unique checkbox on my feeds importer mapper. It is because the field validation rule is on a product reference field (supplied by commerce module).

The problem is that in field_validation_feeds_processor_targets_alter, $rule->field_name is used to set the field unique, but the index on the $targets array for the product reference field has either ':sku' or ':product_id' appended to the end of the field name.

2pha’s picture

I got it working..

I thought I should post my code here in case it helps someone in the future.

I had to add this to the existingEntityId function in FeedsProcessor.inc in the Feeds module:

default:
          $ids = module_invoke_all('feeds_existing_entity_id', $target, $value, $this->entityType(), $this->bundle());
          $entity_id = reset(array_filter($ids));

I then changed the functions in field_validation.feeds.inc to:


function field_validation_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
  module_load_include('inc', 'ctools', 'includes/export');
  $rules = ctools_export_load_object('field_validation_rule', 'conditions', array('entity_type' => $entity_type, 'bundle' => $bundle_name, 'validator' => 'field_validation_unique_validator'));
  foreach ($rules as $rule) {
    if (!empty($rule->disabled)) {
      continue;
    }
    if (!empty($targets[$rule->field_name])) {
      $targets[$rule->field_name]['optional_unique'] = TRUE;
      $targets[$rule->field_name]['unique_callbacks'][] = 'field_validation_feeds_existing_entity_id';
      $targets[$rule->field_name]['bundle_name'] = $bundle_name;
    }
    if (!empty($targets[$rule->field_name.':sku'])) {
      $targets[$rule->field_name.':sku']['optional_unique'] = TRUE;
      $targets[$rule->field_name.':sku']['unique_callbacks'][] = 'field_validation_feeds_existing_entity_id';
      $targets[$rule->field_name.':sku']['bundle_name'] = $bundle_name;
    }
    if (!empty($targets[$rule->field_name.':product_id'])) {
      $targets[$rule->field_name.':product_id']['optional_unique'] = TRUE;
      $targets[$rule->field_name.':product_id']['unique_callbacks'][] = 'field_validation_feeds_existing_entity_id';
      $targets[$rule->field_name.':product_id']['bundle_name'] = $bundle_name;
    }
  }
}

function field_validation_feeds_existing_entity_id($target, $value, $entity_type, $bundle_name) {
  module_load_include('inc', 'ctools', 'includes/export');
  $target = preg_replace('/:sku$/', '', $target);
  $target = preg_replace('/:product_id$/', '', $target);
  $unique_rules = ctools_export_load_object('field_validation_rule', 'conditions', array('entity_type' => $entity_type, 'bundle' => $bundle_name, 'field_name' => $target, 'validator' => 'field_validation_unique_validator'));
  $unique_rule = reset($unique_rules);
  if (!empty($unique_rule)) {
    // Get unique entity ID from unique field value. Note that we are not using
    // field_validation_unique_validator::validate() because we don't want to
    // fire set_error().
    if($target == 'field_product'){
      $product = commerce_product_load_by_sku($value);
      $value = $product->product_id;
    }
    $query = new EntityFieldQuery();
    $result = $query->entityCondition('entity_type', $entity_type)
      ->entityCondition('bundle', $bundle_name)
      ->fieldCondition($target, $unique_rule->col, $value, '=')
      // Run the query as user 1.
      ->addMetaData('account', user_load(1))
      ->execute();
    if (isset($result[$entity_type])) {
      $ids = array_keys($result[$entity_type]);
      return reset($ids);
    }
  }
}

In the above code I have hard coded the name of my product reference field 'field_product'.

Again, I'm not sure if this is really the right place to post this, but I felt I needed to post it somewhere as I'm pretty sure someone else will face this issue at some point.

emilyf’s picture

I got this working through the method I put in #139 here: http://drupal.org/node/661606#comment-7574929

emilyf’s picture

Note that this did not work with the dev branch but it did with 7.x-2.3

jmev’s picture

emilyf, I'm not sure I followed your post clearly, but are you saying it would work to just bypass suggestions on this page and follow steps in your post?

jmev’s picture

Issue summary: View changes

Updated issue summary.

MegaChriz’s picture

Issue summary: View changes
Status: Active » Needs review
Related issues: +#1923064: Feeds unique based on property validation?
FileSize
3.65 KB

A fix for unique target in mappers has finally been committed to Feeds! However, the Field validation module's implementation for this feature in Feeds is based on an older patch. Specifically, the parameters for the unique callback were changed to line them up with similar callback functions in Feeds. As such, the implementation in Field validation is outdated.

Patch attached. This is the same patch as in #661606-195: Support unique targets in mappers.

ar-jan’s picture

I have tested fields_validation with the patch in #18 (in combination with feeds 7.x-2.0-alpha8+46-dev (2014-Aug-29)) and can confirm that the patch works.

Tested with a CSV import to node, using a text field configured to be Unique per bundle.

Also note the docs by MegaChriz.

Thanks, +1.

Leeteq’s picture

aegirone’s picture

#18 is ok . I have test this problem , all is ok ............

g089h515r806’s picture

Status: Needs review » Fixed

commited.

Status: Fixed » Closed (fixed)

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