EntityConstraintViolationList::filterByFields

public EntityConstraintViolationList::filterByFields(array $field_names)

Filters this violation list by the given fields.

The returned object just has violations attached to the provided fields.

When violations should be displayed for a sub-set of visible fields only, this method may be used to filter the set of visible violations first.

Parameters

string[] $field_names: The names of the fields to filter violations for.

Return value

$this

Overrides EntityConstraintViolationListInterface::filterByFields

File

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

Class

EntityConstraintViolationList
Implements an entity constraint violation list.

Namespace

Drupal\Core\Entity

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.