getEntityViolations example

/** * {@inheritdoc} */
  protected function buildErrorObjects(HttpException $exception) {
    /** @var \Drupal\jsonapi\Exception\UnprocessableHttpEntityException $exception */
    $errors = parent::buildErrorObjects($exception);
    $error = $errors[0];
    unset($error['links']);

    $errors = [];
    $violations = $exception->getViolations();
    $entity_violations = $violations->getEntityViolations();
    foreach ($entity_violations as $violation) {
      /** @var \Symfony\Component\Validator\ConstraintViolation $violation */
      $error['detail'] = 'Entity is not valid: '
        . $violation->getMessage();
      $error['source']['pointer'] = '/data';
      $errors[] = $error;
    }

    $entity = $violations->getEntity();
    foreach ($violations->getFieldNames() as $field_name) {
      $field_violations = $violations->getByField($field_name);
      
    // a pending revision. This is needed so validation constraints like     // \Drupal\path\Plugin\Validation\Constraint\PathAliasConstraintValidator     // know in advance (before hook_entity_presave()) that the new revision will     // be a pending one.     if ($this->workspaceManager->hasActiveWorkspace()) {
      $form['#entity_builders'][] = [static::class, 'entityFormEntityBuild'];
    }

    // Run the workspace conflict validation constraint when the entity form is     // being built so we can "disable" it early and display a message to the     // user, instead of allowing them to enter data that can never be saved.     foreach ($entity->validate()->getEntityViolations() as $violation) {
      if ($violation->getConstraint() instanceof EntityWorkspaceConflictConstraint) {
        $form['#markup'] = $violation->getMessage();
        $form['#access'] = FALSE;
        continue;
      }
    }
  }

  /** * Entity builder that marks all supported entities as pending revisions. */
  

  protected function flagViolations(EntityConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) {
    // Flag entity level violations.     foreach ($violations->getEntityViolations() as $violation) {
      /** @var \Symfony\Component\Validator\ConstraintViolationInterface $violation */
      $form_state->setErrorByName(str_replace('.', '][', $violation->getPropertyPath())$violation->getMessage());
    }
    // Let the form display flag violations of its fields.     $this->getFormDisplay($form_state)->flagWidgetsErrorsFromViolations($violations$form$form_state);
  }

  /** * Initializes the form state and the entity before the first form build. * * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */
return $extracted;
  }

  /** * {@inheritdoc} */
  public function validateFormValues(FieldableEntityInterface $entity, array &$form, FormStateInterface $form_state) {
    $violations = $entity->validate();
    $violations->filterByFieldAccess();

    // Flag entity level violations.     foreach ($violations->getEntityViolations() as $violation) {
      /** @var \Symfony\Component\Validator\ConstraintViolationInterface $violation */
      $form_state->setError($form$violation->getMessage());
    }

    $this->flagWidgetsErrorsFromViolations($violations$form$form_state);
  }

  /** * {@inheritdoc} */
  public function flagWidgetsErrorsFromViolations(EntityConstraintViolationListInterface $violations, array &$form, FormStateInterface $form_state) {
    
Home | Imprint | This part of the site doesn't use cookies.