You are here

class FeedImportMergeNoDuplicates in Feed Import 8

Class which merges field values checking for duplicates. Default merge mode.

Hierarchy

Expanded class hierarchy of FeedImportMergeNoDuplicates

File

feed_import_base/src/FeedImportMergeNoDuplicates.php, line 8

Namespace

Drupal\feed_import_base
View source
class FeedImportMergeNoDuplicates extends FeedImportMergeField {

  /**
   * {@inheritdoc}
   */
  public function merge(array &$current, array &$new, array &$field) {
    $changed = FALSE;
    $cnt = count($new);
    if ($cnt == count($current)) {

      // Same number of items, it is possible that the
      // field is still the same
      // Just check if the items are the same.
      for ($i = 0; $i < $cnt; $i++) {
        if (!$field['compare']($new[$i], $current[$i])) {
          $changed = TRUE;
          break;
        }
      }
    }
    else {

      // Different number of items, the field is 100% changed.
      $changed = TRUE;
    }
    if ($changed) {

      // Get the new field values, but try to copy the existing ones.
      $temp = array();
      for ($i = 0; $i < $cnt; $i++) {
        $not_found = TRUE;
        foreach ($current as $key => &$val) {

          // Compare by function.
          if ($field['compare']($new[$i], $val)) {

            // Value is the same, just get a reference.
            $temp[] =& $val;
            $not_found = FALSE;
            break;
          }
        }
        unset($val);
        if ($not_found) {

          // The value is new.
          $temp[] =& $new[$i];
        }
      }

      // Set current.
      $current = $temp;
    }
    return $changed;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedImportMergeField::overwriteEmpty public function Remove field if is missing from source. 1
FeedImportMergeNoDuplicates::merge public function Merge the new and current field values. The merge should be set in $current ref. variable Overrides FeedImportMergeField::merge