function EntityConstraintViolationList::filterByFields

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Entity/EntityConstraintViolationList.php \Drupal\Core\Entity\EntityConstraintViolationList::filterByFields()
  2. 10 core/lib/Drupal/Core/Entity/EntityConstraintViolationList.php \Drupal\Core\Entity\EntityConstraintViolationList::filterByFields()
  3. 11.x core/lib/Drupal/Core/Entity/EntityConstraintViolationList.php \Drupal\Core\Entity\EntityConstraintViolationList::filterByFields()

Overrides EntityConstraintViolationListInterface::filterByFields

1 call to EntityConstraintViolationList::filterByFields()
EntityConstraintViolationList::filterByFieldAccess in core/lib/Drupal/Core/Entity/EntityConstraintViolationList.php
Filters this violation list to apply for accessible fields only.

File

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

Class

EntityConstraintViolationList
Implements an entity constraint violation list.

Namespace

Drupal\Core\Entity

Code

public function filterByFields(array $field_names) {
    $this->groupViolationOffsets();
    $new_violations = [];
    foreach (array_intersect_key($this->violationOffsetsByField, array_flip($field_names)) as $field_name => $offsets) {
        foreach ($offsets as $offset) {
            $violation = $this->get($offset);
            // Take care of composite field violations and re-map them to some
            // covered field if necessary.
            if ($violation->getConstraint() instanceof CompositeConstraintBase) {
                $covered_fields = $violation->getConstraint()
                    ->coversFields();
                // Keep the composite field if it covers some remaining field and put
                // a violation on some other covered field instead.
                if ($remaining_fields = array_diff($covered_fields, $field_names)) {
                    $message_params = [
                        '%field_name' => $field_name,
                    ];
                    $violation = new ConstraintViolation($this->t('The validation failed because the value conflicts with the value in %field_name, which you cannot access.', $message_params), 'The validation failed because the value conflicts with the value in %field_name, which you cannot access.', $message_params, $violation->getRoot(), reset($remaining_fields), $violation->getInvalidValue(), $violation->getPlural(), $violation->getCode(), $violation->getConstraint(), $violation->getCause());
                    $new_violations[] = $violation;
                }
            }
            $this->remove($offset);
        }
    }
    foreach ($new_violations as $violation) {
        $this->add($violation);
    }
    return $this;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.