Add fallback option to redirect formatter when target link does not exist

Created on 13 June 2025, 24 days ago

Problem/Motivation

Currently, when using a formatter to redirect a node to a link defined in an entity reference or link field, an error occurs if the target link does not exist or if an alias is mistakenly used instead of an actual reference. This can break the user experience and lead to 404 or fatal errors.

There is no built-in fallback mechanism to handle missing or invalid links gracefully.

Steps to reproduce

  1. Create a content type with a link or entity reference field used for redirection.
  2. Configure a formatter to redirect to that field.
  3. Enter a non-existent alias.
  4. Visit the node page.

Observe the error or broken redirect behavior.

Proposed resolution

Add an additional option to the redirect formatter configuration that defines what to do when the target link is invalid or missing. Possible fallback options could include:

  • Redirect to the site's front page
  • Show "Access Denied" or "Page not found"
  • Display a customizable message or error page
  • Skip redirect and display the original node content

Remaining tasks

Update the formatter plugin to include a fallback setting.

  • Implement logic to detect invalid or non-resolvable links.
  • Add configuration options to the formatter UI.
  • Write tests for each fallback scenario.
  • Update documentation.

User interface changes

In the formatter settings for the redirect field, a new select field will be added:
"If link does not exist:"
Options:

  • Redirect to front page
  • Show 403 (Access Denied)
  • Show 404 (Page not found)
  • No redirect (display node content)

It is good implemented on the Rabbit hole https://www.drupal.org/project/rabbit_hole β†’

πŸ› Bug report
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡¦Ukraine Foxy-vikvik

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024