How restrict a constraints to a content type?

by bkildow   Last Updated April 20, 2018 01:07 AM

I would like to create validation for a several fields on a content type to check if they each have unique values from each other. The part I'm struggling with is how to add the constraint for just my content type, and not all node entities. So far I'm using hook_entity_type_build() to add: $entity_types['node']->addConstraint('UniqueSizes', []); Is there a way to restrict this to only my content type?

Tags : 8 entities

Answers 2

No, it's not directly possibly when adding the constraint.

But you can always check the bundle or existence of certain fields (in some places preferable, if you ever need another node type with those fields and the same logic) within your constraint when it is run.

July 25, 2016 19:50 PM

If you need to add constraints to fields added to a bundle, you can implement hook_entity_bundle_field_info_alter(), which gets the bundle as last parameter.

Keep in mind that, once the changes in Finalize API for creating, overriding, and altering code-defined bundle fields are committed, code implementing hook_entity_bundle_field_info_alter() would probably need to be changed.

July 25, 2016 20:14 PM

Related Questions

How to create a custom template for entities?

Updated April 07, 2015 03:03 AM

Drupal 8 Custom Entities from a UI?

Updated July 14, 2015 15:03 PM