You are here

public static function Field::getDuplicateValues in Helper 8

Finds duplicate field values.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field list class.

string $property: The field item property to use. Defaults to mainPropertyName() on the field class if not provided.

Return value

mixed[] An array of duplicate field values.

1 call to Field::getDuplicateValues()
FieldListUniqueValuesValidator::validate in src/Plugin/Validation/Constraint/FieldListUniqueValuesValidator.php
Checks if the passed value is valid.

File

src/Field.php, line 24

Class

Field
Provides helpers for working with field values.

Namespace

Drupal\helper

Code

public static function getDuplicateValues(FieldItemListInterface $items, $property = NULL) {
  $values = [];
  foreach ($items as $delta => $item) {

    /** @var \Drupal\Core\Field\FieldItemInterface $item */
    if (!isset($property)) {
      $property = $item::mainPropertyName();
    }
    if (isset($item->{$property})) {
      $values[] = (string) $item->{$property};
    }
  }
  $value_counts = array_count_values($values);
  $duplicates = array_filter($value_counts, function ($count) {
    return $count > 1;
  });
  return array_keys($duplicates);
}