How to disable or improve Constraint Violation

Created on 4 April 2024, about 1 year ago
Updated 10 September 2024, 7 months ago

Problem/Motivation

Hi, I'm trying to replace the error message "This entity (user: 12) cannot be referenced." by a more appropriate message like : "The user test@test.com cannot be added. Maybe his account is blocked."

Steps to reproduce

On a content type, juste add a reference field which point to a view which give you a list of your active user.
Create a new node using your content type and ddd some of your active user. Save you new node.
Then go switch status to blocked for some of the user you added to your node.
Go back to edit your node but juste save it, don't make any change.
You will get a message saying "This entity (user: 12) cannot be referenced." (the 12 is an example of user ID).
If you have allow many user to be defined on your content type, the message will repeat as many time as you have user blocked.

Proposed resolution

- let the dev create there own constraint violation and hiding existing constraint violation
- create a page to let the user customize existing constraint violation

Just for people to know, I have already created my own constraint violation for this case but I can't hide the already existing constraint violation... So now I've got the 2 messages :
"This entity (user: 12) cannot be referenced.
The user test@test.com cannot be added. Maybe his account is blocked."

I've found on stackexchange that somebody told to edit the list of constraint violation with a hook_validation_constraint_alter(). But I didn't found how to use it properly....
I have also found the hook_entity_type_alter() example in the project
remove_entity_untranslatable_field_validation but when I tried, it disable the ValidReferenceConstraint on the page and not only on the field I have added... so I've got an error which say some field like date, address and other needed it.

So does somebody have a solution for this?

NB : sorry I'm not english, so I could have made some mistake on the support request.

💬 Support request
Status

Closed: works as designed

Version

9.5

Component
Entity reference 

Last updated 8 days ago

No maintainer
Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Clemchan
  • Drupal 9 was end-of-life in November 2023.

  • Status changed to Closed: works as designed 7 months ago
  • 🇳🇿New Zealand quietone

    @Clemchan. The Drupal Core issue queue is not the ideal place for support requests. The 'support request' option is there for filing support issues for contributed modules and themes. There are several support options listed on our support page (Community > Support at the top of Drupal.org) and there is Drupal Slack . You may get better replies in one of those places.

Production build 0.71.5 2024