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?
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.
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.