Problem/Motivation
The short code in /nc/{code}, /uc/{code}, /tc/{code}, /rc/{code} (and the /ns/, /us/, /ts/, /rs/ variants) is not persisted anywhere. It is the entity's ID in base 36 — generated as base_convert($entity->id(), 10, 36) and decoded back with intval($short_value, 36).
For nodes, users and terms that is mostly harmless: if the entity is gone there is nothing to point at. For the redirect entity type it is a data-loss defect, because a redirect entity is designed to be disposable. redirect_entity_predelete() deletes every redirect pointing at a deleted entity; redirect_path_alias_insert()/_update() delete redirects whose source matches a newly created alias; site-specific cleanup jobs and the bulk form on the redirect list delete them routinely. A short URL that has been printed on a flyer or encoded in a QR code therefore dies silently — and it cannot be restored, because recreating the same redirect yields a new entity ID and hence a different code.
The fallback makes it worse: when a code cannot be resolved, CodeController::resolveRedirectUrl() returns Url::fromRoute(''), so a dead permalink lands on the front page rather than erroring. Nobody notices until a visitor complains.
On one production site this has recurred over four years; seven printed codes were lost and had to be worked around by hand-creating a redirect whose source is literally rc/.
Steps to reproduce
- Enable the short URL / QR code for the redirect entity type.
- Create a redirect, note the generated /rc/
, confirm it resolves. - Delete the redirect — or delete the node it points at, which makes redirect_entity_predelete() delete it for you.
- /rc/
now rreturns 404 - Recreate an identical redirect: it gets a new ID, so the original code stays dead.
Proposed resolution
Persist the code in a table owned by this module and resolve against it, keeping the ID-derived value as a fallback so nothing already printed changes:
- New autoshortqr.install with a hook_schema() table autoshortqr_code: entity_type, code, entity_id (nullable), target_uri, label, created, orphaned. Primary key (entity_type, code), index (entity_type, entity_id).
- New CodeRepository service:
- codeFor($entity) — the stored code if present, otherwise base_convert($entity->id(), 10, 36). This keeps existing sites byte-identical and makes a data migration unnecessary.
- lookup($entity_type, $code), record($entity), tombstone($entity). - hook_entity_insert() records the code; hook_entity_update() refreshes the stored target; hook_entity_predelete() marks the row orphaned and freezes the last known target.
- CodeController resolves in three steps: live row → orphaned row (redirect to the frozen target) → legacy intval($code, 36) load. Step three preserves every code generated by 4.1.1 and earlier.
- A small admin list so an orphaned code can be retargeted — otherwise it is frozen rather than fixed.
Remaining tasks
- Tests.
User interface changes
New admin listing of issued codes. No change to existing forms.
API changes
New autoshortqr.code_repository service. The base_convert() call sites are replaced by codeFor(): autoshortqr.module:317, autoshortqr.module:576, ShortLinkLinkItem.php:87, AutoCodeLinkItem.php:86, AutoCodeField.php:45.
Data model changes
New table autoshortqr_code. No migration required — the legacy fallback covers pre-existing codes.
Issue fork autoshortqr-3621484
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #4
guillaumepacilly commentedComment #6
guillaumepacilly commented