diff --git a/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php b/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php index 4ffa1f2..5a6551a 100644 --- a/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php +++ b/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php @@ -274,34 +274,35 @@ protected function movePropertyPathViolationsRelativeToField($field_name, Constr // All this code below is done in order to change the property path of the // violations to be relative to the item list, so like title.0.value gets // changed to 0.value. Sadly constraints in symfony don't have setters so - // we have to create new objects. On top of that, the interface is missing - // getConstraint() and getCause(), so we have to rely on a specific - // constraint implementation. - if ($violation instanceof ConstraintViolation) { - /** @var \Symfony\Component\Validator\ConstraintViolation $violation */ - // Create a new violation object with just a different property path. - $violation_path = $violation->getPropertyPath(); - $path_parts = explode('.', $violation_path); - if ($path_parts[0] === $field_name) { - unset($path_parts[0]); - } - $new_path = implode('.', $path_parts); - $new_violation = new ConstraintViolation( - $violation->getMessage(), - $violation->getMessageTemplate(), - $violation->getMessageParameters(), - $violation->getRoot(), - $new_path, - $violation->getInvalidValue(), - $violation->getMessagePluralization(), - $violation->getCode(), - $violation->getConstraint(), - $violation->getCause() - ); + // we have to create new objects. + /** @var \Symfony\Component\Validator\ConstraintViolationInterface $violation */ + // Create a new violation object with just a different property path. + $violation_path = $violation->getPropertyPath(); + $path_parts = explode('.', $violation_path); + if ($path_parts[0] === $field_name) { + unset($path_parts[0]); } - else { - $new_violation = $violation; + $new_path = implode('.', $path_parts); + + $constraint = NULL; + $cause = NULL; + if ($violation instanceof ConstraintViolation) { + $constraint = $violation->getConstraint(); + $cause = $violation->getCause(); } + + $new_violation = new ConstraintViolation( + $violation->getMessage(), + $violation->getMessageTemplate(), + $violation->getMessageParameters(), + $violation->getRoot(), + $new_path, + $violation->getInvalidValue(), + $violation->getMessagePluralization(), + $violation->getCode(), + $constraint, + $cause + ); $new_violations->add($new_violation); } return $new_violations; diff --git a/core/lib/Drupal/Core/Entity/EntityConstraintViolationList.php b/core/lib/Drupal/Core/Entity/EntityConstraintViolationList.php index 3a28472..6edd31c 100644 --- a/core/lib/Drupal/Core/Entity/EntityConstraintViolationList.php +++ b/core/lib/Drupal/Core/Entity/EntityConstraintViolationList.php @@ -24,13 +24,13 @@ class EntityConstraintViolationList extends ConstraintViolationList implements E protected $entity; /** - * Violations grouped by field. + * Violation offsets grouped by field. * * Keys are field names, values are arrays of violation offsets. * * @var array[]|null */ - protected $violationsByField; + protected $violationOffsetsByField; /** * {@inheritdoc} @@ -45,25 +45,25 @@ public function __construct(array $violations = array(), FieldableEntityInterfac } /** - * Groups violations by field and sets the $violationsByField property. + * Groups violation offsets by field and sets the $violationsByField property. * * @return array[] * The violations grouped by field. Keys are field names, values are arrays * of violation offsets. */ - protected function groupViolationsbyField() { - if (!isset($this->violationsByField)) { - $this->violationsByField = []; + protected function violationOffsetsByField() { + if (!isset($this->violationOffsetsByField)) { + $this->violationOffsetsByField = []; foreach ($this as $offset => $violation) { if ($path = $violation->getPropertyPath()) { list($field_name) = explode('.', $path, 2); if ($this->entity->hasField($field_name)) { - $this->violationsByField[$field_name][$offset] = $offset; + $this->violationOffsetsByField[$field_name][$offset] = $offset; } } } } - return $this->violationsByField; + return $this->violationOffsetsByField; } /** @@ -77,18 +77,18 @@ public function filterByField($field_name) { * {@inheritdoc} */ public function filterByFields(array $field_names) { - if (!isset($this->violationsByField)) { - $this->groupViolationsbyField(); + if (!isset($this->violationOffsetsByField)) { + $this->violationOffsetsByField(); } $violations = []; - foreach (array_intersect_key($this->violationsByField, array_flip($field_names)) as $field_name => $offsets) { + foreach (array_intersect_key($this->violationOffsetsByField, array_flip($field_names)) as $field_name => $offsets) { foreach ($offsets as $offset) { $violations[] = $this->get($offset); // Use parent implementation in order to avoid unnecessary rebuilds of // the violations by field array. parent::remove($offset); } - unset($this->violationsByField[$field_name]); + unset($this->violationOffsetsByField[$field_name]); } return new ConstraintViolationList($violations); } @@ -110,7 +110,7 @@ public function filterByFieldAccess(AccountInterface $account = NULL) { * {@inheritdoc} */ public function getViolatedFieldNames() { - return array_keys($this->groupViolationsbyField()); + return array_keys($this->violationOffsetsByField()); } /** @@ -125,7 +125,7 @@ public function getEntity() { */ public function add(ConstraintViolationInterface $violation) { parent::add($violation); - $this->violationsByField = NULL; + $this->violationOffsetsByField = NULL; } /** @@ -133,7 +133,7 @@ public function add(ConstraintViolationInterface $violation) { */ public function remove($offset) { parent::remove($offset); - $this->violationsByField = NULL; + $this->violationOffsetsByField = NULL; } /** @@ -141,7 +141,7 @@ public function remove($offset) { */ public function set($offset, ConstraintViolationInterface $violation) { parent::set($offset, $violation); - $this->violationsByField = NULL; + $this->violationOffsetsByField = NULL; } }