protected function EntityConstraintViolationList::groupViolationOffsets

protected EntityConstraintViolationList::groupViolationOffsets()

Groups violation offsets by field and entity level.

Sets the $violationOffsetsByField and $entityViolationOffsets properties.

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;
      }
    }
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!EntityConstraintViolationList.php/function/EntityConstraintViolationList::groupViolationOffsets/8.1.x