You are here

class Field in Helper 8

Provides helpers for working with field values.

Hierarchy

  • class \Drupal\helper\Field

Expanded class hierarchy of Field

1 file declares its use of Field
FieldListUniqueValuesValidator.php in src/Plugin/Validation/Constraint/FieldListUniqueValuesValidator.php

File

src/Field.php, line 10

Namespace

Drupal\helper
View source
class Field {

  /**
   * Finds duplicate field values.
   *
   * @param \Drupal\Core\Field\FieldItemListInterface $items
   *   The field list class.
   * @param string $property
   *   The field item property to use. Defaults to mainPropertyName() on the
   *   field class if not provided.
   *
   * @return mixed[]
   *   An array of duplicate field values.
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Field::getDuplicateValues public static function Finds duplicate field values.