Skip to content

About

No description, website, or topics provided.

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Entity Reference Integrity

A Drupal 11 module that detects and optionally repairs broken entity reference fields. It scans every entity_reference and entity_reference_revisions field across all entity types, identifies references that point to targets that no longer exist, and presents the results through both the admin UI and Drush.

What It Detects

  • Missing target entities -- an entity_reference or entity_reference_revisions field value whose target_id points to an entity that has been deleted or never existed.
  • Covers all fieldable entity types (nodes, taxonomy terms, paragraphs, media, users, custom entities, etc.).
  • Handles both base fields and configurable (Field UI) fields.

Installation

  1. Place the module in your Drupal installation under modules/custom/entity_reference_integrity.
  2. Enable the module:
    drush en entity_reference_integrity
  3. Grant the Access entity reference integrity report permission to the roles that should be allowed to view the report.

Usage

Admin UI

Navigate to Administration > Reports > Entity reference integrity (/admin/reports/entity-reference-integrity). The page displays a table of every broken reference found, including:

Column Description
Source entity type Machine name of the entity type that holds the broken field
Source ID Entity ID of the source entity
Field name Machine name of the reference field
Field type entity_reference or entity_reference_revisions
Target entity type Expected target entity type
Target ID The target_id value that cannot be loaded

Drush Commands

Report

drush entity-reference-integrity:report
# Aliases: eri:report, eri-report

Supported output formats (via the --format flag):

Format Flag
Table --format=table (default)
CSV --format=csv
JSON --format=json

Auto-Fix

drush entity-reference-integrity:report --fix

When --fix is passed the command will:

  1. Scan for all broken references.
  2. For each broken reference, nullify the invalid target_id on the source entity and save it.
  3. Print a summary of fixed references.

Warning: The fix operation modifies entity data. Always back up your database before running with --fix.

Batch Processing

Both scanning and fixing process entities in configurable chunks (default: 50 entities per batch) to keep memory usage predictable on large sites. The batch size can be adjusted by overriding the service or extending the IntegrityChecker class.

Architecture

entity_reference_integrity/
  src/
    Service/
      IntegrityChecker.php        -- Core scanning and auto-fix logic
    Controller/
      IntegrityReportController.php -- Admin UI report page
    Commands/
      IntegrityReportCommands.php  -- Drush command (report + --fix)
  tests/
    src/Unit/
      IntegrityCheckerTest.php    -- PHPUnit tests for the checker service
  entity_reference_integrity.info.yml
  entity_reference_integrity.services.yml
  entity_reference_integrity.routing.yml
  entity_reference_integrity.links.menu.yml
  entity_reference_integrity.permissions.yml
  drush.services.yml

Key Classes

  • IntegrityChecker -- Stateless service that scans fields and optionally fixes broken references. Injected with the entity type manager, field manager, and a logger channel.
  • IntegrityReportController -- Drupal controller that renders the scan results as a render array with a table element.
  • IntegrityReportCommands -- Drush 12+ command class that exposes the scan via CLI with sortable, format-aware output and an optional --fix flag.

Requirements

  • Drupal 10 or 11
  • PHP 8.1+
  • Drush 12+ (for CLI commands)

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

No description, website, or topics provided.

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages