protected function EntityFormDisplay::movePropertyPathViolationsRelativeToField

protected EntityFormDisplay::movePropertyPathViolationsRelativeToField($field_name, ConstraintViolationListInterface $violations)

Moves the property path to be relative to field level.

Parameters

string $field_name: The field name.

\Symfony\Component\Validator\ConstraintViolationListInterface $violations: The violations.

Return value

\Symfony\Component\Validator\ConstraintViolationList A new constraint violation list with the changed property path.

File

core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php, line 271

Class

EntityFormDisplay
Configuration entity that contains widget options for all components of a entity form in a given form mode.

Namespace

Drupal\Core\Entity\Entity

Code

protected function movePropertyPathViolationsRelativeToField($field_name, ConstraintViolationListInterface $violations) {
  $new_violations = new ConstraintViolationList();
  foreach ($violations as $violation) {
    // All the logic below is necessary 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.
    /** @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]);
    }
    $new_path = implode('.', $path_parts);

    $constraint = NULL;
    $cause = NULL;
    $parameters = [];
    $plural = NULL;
    if ($violation instanceof ConstraintViolation) {
      $constraint = $violation->getConstraint();
      $cause = $violation->getCause();
      $parameters = $violation->getParameters();
      $plural = $violation->getPlural();
    }

    $new_violation = new ConstraintViolation(
    $violation->getMessage(), 
    $violation->getMessageTemplate(), 
    $parameters, 
    $violation->getRoot(), 
    $new_path, 
    $violation->getInvalidValue(), 
    $plural, 
    $violation->getCode(), 
    $constraint, 
    $cause
    );
    $new_violations->add($new_violation);
  }
  return $new_violations;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!Entity!EntityFormDisplay.php/function/EntityFormDisplay::movePropertyPathViolationsRelativeToField/8.1.x