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;
      }
    }
  }
}
doc_Drupal
2016-10-29 09:04:47
Comments
Leave a Comment

Please login to continue.