function UniqueFieldValueValidator::extractDuplicates

Same name in other branches
  1. 11.x core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/UniqueFieldValueValidator.php \Drupal\Core\Validation\Plugin\Validation\Constraint\UniqueFieldValueValidator::extractDuplicates()

Get an array of duplicate field values.

Parameters

array $item_values: The item values.

Return value

array Item values only for deltas that duplicate an earlier delta.

1 call to UniqueFieldValueValidator::extractDuplicates()
UniqueFieldValueValidator::validate in core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/UniqueFieldValueValidator.php

File

core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/UniqueFieldValueValidator.php, line 144

Class

UniqueFieldValueValidator
Validates that a field is unique for the given entity type.

Namespace

Drupal\Core\Validation\Plugin\Validation\Constraint

Code

private function extractDuplicates(array $item_values) : array {
    $value_frequency = array_count_values($item_values);
    // Filter out item values which are not duplicates while preserving deltas
    $duplicate_values = array_intersect($item_values, array_keys(array_filter($value_frequency, function ($value) {
        return $value > 1;
    })));
    // Exclude the first delta of each duplicate value.
    $first_deltas = array_unique($duplicate_values);
    return array_diff_key($duplicate_values, $first_deltas);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.