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.
- Missing target entities -- an
entity_referenceorentity_reference_revisionsfield value whosetarget_idpoints 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.
- Place the module in your Drupal installation under
modules/custom/entity_reference_integrity. - Enable the module:
drush en entity_reference_integrity
- Grant the Access entity reference integrity report permission to the roles that should be allowed to view the report.
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 entity-reference-integrity:report
# Aliases: eri:report, eri-reportSupported output formats (via the --format flag):
| Format | Flag |
|---|---|
| Table | --format=table (default) |
| CSV | --format=csv |
| JSON | --format=json |
drush entity-reference-integrity:report --fixWhen --fix is passed the command will:
- Scan for all broken references.
- For each broken reference, nullify the invalid
target_idon the source entity and save it. - Print a summary of fixed references.
Warning: The fix operation modifies entity data. Always back up your database before running with
--fix.
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.
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
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--fixflag.
- Drupal 10 or 11
- PHP 8.1+
- Drush 12+ (for CLI commands)
This project is licensed under the MIT License. See the LICENSE file for details.