We are using Media Entity module that provides a Document media bundle. On our Article content type, we have created an Entity Reference field to Media that is set to Document media bundle.

When you click Delete on this Document media entity, you are taken to a Are you sure you want to delete the media <media name>? page for confirmation (not sure of the official name, but let's call it Confirm Deletion page). On this page we would like to display all the pages where this media entity is currently being referenced.

How would we display the list of nodes that are referencing the media that content editors are attempting to delete?


1 Answer 1


Additionally to the comment, here's the snippet we used based on the entity_usage module in a hook_form_alter():

  $object = $form_state->getFormObject();
  if ($object instanceof ContentEntityDeleteForm) {
    $entity = $object->getEntity();
    $usage_data = \Drupal::service('entity_usage.usage')->listSources($entity);

    if (!empty($usage_data)) {
      $count = 0;

      foreach ($usage_data as $usage) {
        $count += count($usage);

      $form['usage_warning_message'] = [
        '#type' => 'container',
        '#markup' => t('This content is used in <a href="@usage_url" target="_blank">@count</a> places.', [
          '@usage_url' => Url::fromRoute('entity_usage.usage_list', [
            'entity_type' => $entity->getEntityTypeId(),
            'entity_id' => $entity->id(),
          '@count' => $count,
        '#attributes' => [
          'class' => ['messages', 'messages--warning'],
        '#weight' => -100,
  • this was super helpful as it lead to other conversations about the delete form. Additionally, I saw your comment on the other question as well. Commented Sep 25, 2018 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.