AddConstraint() unexpected behaviour

Created on 11 February 2025, 3 months ago

Problem/Motivation

When using a custom constraint defined in a module in multiple modules, the addConstraint() method assumes that the constraint is unique per entity type. Expected behaviour would be that whenever we use the addConstraint() it should not overwrite the existing constraint on the entity type.

Let's say we have defined a nodeLimit constraint in a module that would allow to create only a certain number of nodes of given content type. When we want to use this constraint in multiple modules, we assume that using addConstraint() in module1_entity_type_alter() and module2_entity_type_alter() preprocess function, the constraint should be applied correctly to both content types. But in our case, only the module2 constraint works.

Steps to reproduce

  • Create a constraint (for example limit the number of nodes of a content type) in module0.
  • Add the constraint from module0 using addConstraint() in module1 and module2 in preprocess module_entity_type_alter().
  • Now the constraint only works for one of the modules.

Proposed resolution

If addConstraint() should be used on a single content type, the documentation should make it clearer.
Our workaround was to overwrite addConstraint() in the custom constraint to accept arrays so for example addConstraint(['node1', 'node2'])

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Active

Version

10.3

Component

entity system

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024