You are here

protected function EntityConstraintViolationList::groupViolationOffsets in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Entity/EntityConstraintViolationList.php \Drupal\Core\Entity\EntityConstraintViolationList::groupViolationOffsets()

Groups violation offsets by field and entity level.

Sets the $violationOffsetsByField and $entityViolationOffsets properties.

4 calls to EntityConstraintViolationList::groupViolationOffsets()
EntityConstraintViolationList::filterByFields in core/lib/Drupal/Core/Entity/EntityConstraintViolationList.php
Filters this violation list by the given fields.
EntityConstraintViolationList::getByFields in core/lib/Drupal/Core/Entity/EntityConstraintViolationList.php
Gets the violations of the given fields.
EntityConstraintViolationList::getEntityViolations in core/lib/Drupal/Core/Entity/EntityConstraintViolationList.php
Gets violations flagged on entity level, not associated with any field.
EntityConstraintViolationList::getFieldNames in core/lib/Drupal/Core/Entity/EntityConstraintViolationList.php
Returns the names of all violated fields.

File

core/lib/Drupal/Core/Entity/EntityConstraintViolationList.php, line 60

Class

EntityConstraintViolationList
Implements an entity constraint violation list.

Namespace

Drupal\Core\Entity

Code

protected function groupViolationOffsets() {
  if (!isset($this->violationOffsetsByField)) {
    $this->violationOffsetsByField = [];
    $this->entityViolationOffsets = [];
    foreach ($this as $offset => $violation) {
      if ($path = $violation
        ->getPropertyPath()) {

        // An example of $path might be 'title.0.value'.
        list($field_name) = explode('.', $path, 2);
        if ($this->entity
          ->hasField($field_name)) {
          $this->violationOffsetsByField[$field_name][$offset] = $offset;
        }
        else {
          $this->entityViolationOffsets[$offset] = $offset;
        }
      }
      else {
        $this->entityViolationOffsets[$offset] = $offset;
      }
    }
  }
}