This does what I want.
The Agenda Node cannot be deleted if there are any Meeting Nodes that refer to it.
Ideally, I’d have preferred that the validation handler would be called when the user clicked the Delete on the primary tasks tabs whereas it is called on the confirmation form. Good enough, though.
use Drupal\Core\Form\FormStateInterface;
function agenda_form_alter(&$form, FormStateInterface $form_state, $form_id) {
switch ($form_id) {
// ...
case "node_agenda_delete_form":
$form['#validate'][] = '_agenda_delete_validation';
break;
// ...
}
}
function _agenda_delete_validation(&$form, FormStateInterface $form_state) {
$agenda_node = $form_state->getFormObject()->getEntity();
$agenda_name = $agenda_node->getTitle();
$agenda_node_id = $agenda_node->id();
$query = \Drupal::entityQuery('node')
->condition('type', 'meeting')
->condition('field_agenda', $agenda_node_id);
$nids = $query->execute();
$count_nids = count($nids);
if ($count_nids > 0) {
$msg_stem = ($count_nids == 1) ? 'There is one Meeting that references "' : 'There are '.$count_nids.' Meetings that reference "';
$form_state->setError($form, $msg_stem.$agenda_name.'". It cannot be deleted yet.' );
}
}
With many thanks to @Clive and @Berdir who gave great answers that I was able to follow up.