You can add custom validation for Drupal entities by adding "constraints" (a concept from Symfony, the PHP framework on top of which Drupal is built) to the entity (using hook_entity_type_alter) or its individual fields (using hook_entity_bundle_field_info_alter). There are a number of different constraint classes already defined (UniqueField, Regex, etc.), or you could create your own.

This is better than adding validation on the node edit form, for example, since it will work anytime an entity is saved, such as when creating one programmatically.

Read more

In Drupal "entity queries", you can filter using fields from referenced entities. This will effectively do a SQL join on the query. For example:

$query = \Drupal::entityQuery('node')
  ->condition('uid.entity:user.mail', '%@example.com', 'LIKE');
Read more
Subscribe to Drupal