3 namespace Drupal\Core\Entity;
5 use Drupal\Core\Entity\Plugin\Validation\Constraint\CompositeConstraintBase;
6 use Drupal\Core\Session\AccountInterface;
7 use Drupal\Core\StringTranslation\StringTranslationTrait;
8 use Symfony\Component\Validator\ConstraintViolation;
9 use Symfony\Component\Validator\ConstraintViolationInterface;
10 use Symfony\Component\Validator\ConstraintViolationList;
13 * Implements an entity constraint violation list.
15 class EntityConstraintViolationList extends ConstraintViolationList implements EntityConstraintViolationListInterface {
17 use StringTranslationTrait;
20 * The entity that has been validated.
22 * @var \Drupal\Core\Entity\FieldableEntityInterface
27 * Violations offsets of entity level violations.
31 protected $entityViolationOffsets;
34 * Violation offsets grouped by field.
36 * Keys are field names, values are arrays of violation offsets.
40 protected $violationOffsetsByField;
45 * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
46 * The entity that has been validated.
47 * @param array $violations
48 * The array of violations.
50 public function __construct(FieldableEntityInterface $entity, array $violations = []) {
51 parent::__construct($violations);
52 $this->entity = $entity;
56 * Groups violation offsets by field and entity level.
58 * Sets the $violationOffsetsByField and $entityViolationOffsets properties.
60 protected function groupViolationOffsets() {
61 if (!isset($this->violationOffsetsByField)) {
62 $this->violationOffsetsByField = [];
63 $this->entityViolationOffsets = [];
64 foreach ($this as $offset => $violation) {
65 if ($path = $violation->getPropertyPath()) {
66 // An example of $path might be 'title.0.value'.
67 list($field_name) = explode('.', $path, 2);
68 if ($this->entity->hasField($field_name)) {
69 $this->violationOffsetsByField[$field_name][$offset] = $offset;
73 $this->entityViolationOffsets[$offset] = $offset;
82 public function getEntityViolations() {
83 $this->groupViolationOffsets();
85 foreach ($this->entityViolationOffsets as $offset) {
86 $violations[] = $this->get($offset);
88 return new static($this->entity, $violations);
94 public function getByField($field_name) {
95 return $this->getByFields([$field_name]);
101 public function getByFields(array $field_names) {
102 $this->groupViolationOffsets();
104 foreach (array_intersect_key($this->violationOffsetsByField, array_flip($field_names)) as $field_name => $offsets) {
105 foreach ($offsets as $offset) {
106 $violations[] = $this->get($offset);
109 return new static($this->entity, $violations);
115 public function filterByFields(array $field_names) {
116 $this->groupViolationOffsets();
117 $new_violations = [];
118 foreach (array_intersect_key($this->violationOffsetsByField, array_flip($field_names)) as $field_name => $offsets) {
119 foreach ($offsets as $offset) {
120 $violation = $this->get($offset);
121 // Take care of composite field violations and re-map them to some
122 // covered field if necessary.
123 if ($violation->getConstraint() instanceof CompositeConstraintBase) {
124 $covered_fields = $violation->getConstraint()->coversFields();
126 // Keep the composite field if it covers some remaining field and put
127 // a violation on some other covered field instead.
128 if ($remaining_fields = array_diff($covered_fields, $field_names)) {
129 $message_params = ['%field_name' => $field_name];
130 $violation = new ConstraintViolation(
131 $this->t('The validation failed because the value conflicts with the value in %field_name, which you cannot access.', $message_params),
132 'The validation failed because the value conflicts with the value in %field_name, which you cannot access.',
134 $violation->getRoot(),
135 reset($remaining_fields),
136 $violation->getInvalidValue(),
137 $violation->getPlural(),
138 $violation->getCode(),
139 $violation->getConstraint(),
140 $violation->getCause()
142 $new_violations[] = $violation;
146 $this->remove($offset);
149 foreach ($new_violations as $violation) {
150 $this->add($violation);
158 public function filterByFieldAccess(AccountInterface $account = NULL) {
159 $filtered_fields = [];
160 foreach ($this->getFieldNames() as $field_name) {
161 if (!$this->entity->get($field_name)->access('edit', $account)) {
162 $filtered_fields[] = $field_name;
165 return $this->filterByFields($filtered_fields);
171 public function getFieldNames() {
172 $this->groupViolationOffsets();
173 return array_keys($this->violationOffsetsByField);
179 public function getEntity() {
180 return $this->entity;
186 public function add(ConstraintViolationInterface $violation) {
187 parent::add($violation);
188 $this->violationOffsetsByField = NULL;
189 $this->entityViolationOffsets = NULL;
195 public function remove($offset) {
196 parent::remove($offset);
197 $this->violationOffsetsByField = NULL;
198 $this->entityViolationOffsets = NULL;
204 public function set($offset, ConstraintViolationInterface $violation) {
205 parent::set($offset, $violation);
206 $this->violationOffsetsByField = NULL;
207 $this->entityViolationOffsets = NULL;