This Drupal 8 module accompanies a Code Enigma blog post. It illustrates how to use the Drupal 8 Entity Validation API to add different validation constraints to the entity creation process.
Blog post: Introduction to Drupal 8 Entity Validation
The examples are kept short and avoid anything that is not related to the actual topic of entity validation.
This example limits the creation of nodes of the type article to a specified number (2).
In a real-world scenario this limit would probably be configured using a configuration form, or specified with a config variable in settings.php.
The module does the following:
-
Defines a new constraint:
ArticleLimitConstraint
. -
Defines a new constraint validator:
ArticleLimitConstraintValidator
-
Attaches the constraint to entities of the type node using a
hook_entity_type_alter
implementation.
To test this functionality:
- Enable the module.
- Create 2 articles.
- Attempt to create a 3rd article and notice the error message.
Coming soon.