This code is in my agenda.module
. The where-used relation is used as follows in the form validation handler that is called when the user clicks the Delete button on the delete confirmation form:
The Agenda Node cannot be deleted if there are any Meeting Nodes that refer to it.
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.' );
}
}