When saving / updating node, i need to validate if certain fields combination is unique. Unique field module is supposed to allow this, but that feature is not working (validating only single field). So i created my own hook:
function mymodule_node_presave(\Drupal\Core\Entity\EntityInterface $node) {
if ($node->bundle() == 'mycontent') {
$term = \Drupal\taxonomy\Entity\Term::load($node->get('field_mytaxonomy')->target_id);
$query = \Drupal::entityQuery('node');
$query->condition('type', 'mycontent');
$query->condition('field_mydate', $node->get('field_mydate')->value, '=');
$query->condition('field_mytaxonomy', $term->id(), '=');
$nids = $query->execute();
if ($nids) {
drupal_set_message('Fields are not unique!', 'error');
// I need to prevent saving of node here
}
}
}
Detection of node with duplicite values being created works. How do i prevent saving that node?