Problem/Motivation

When using the checkInternalAnchors function to verify URLs in a body field, discrepancies arise in environments where a base path (e.g., /d9dev) is added by the web server (such as Apache2) but is not part of the original URLs stored in the database.

For example:

Production Environment: URLs are stored as href="/node/1".
Development Environment: The site is served under a base path (e.g., /d9dev), requiring links to be prefixed (e.g., href="/d9dev/node/1").
This inconsistency causes the function to fail in detecting and validating relative links in environments where a base path is used.

Steps to reproduce

  • Backport a production database with links in body fields (href="/node/1") to a development environment served under a base path (e.g., /d9dev). Or manually strip the base path from an internal link.
  • Call the checkInternalAnchors function on the body field content.
  • Obs

erve that the function fails to validate the links because the base path is missing from the href.

Proposed resolution

Modify the checkInternalAnchors function to dynamically detect the base path from the request (\Drupal::request()->getBasePath()) and prepend it to the href attribute when necessary. This ensures that links are properly validated regardless of the environment.

Remaining tasks

Review PR and test.

Comments

smulvih2 created an issue. See original summary.

smulvih2’s picture

PR: https://git.drupalcode.org/project/safedelete/-/merge_requests/8

The following changes were made:

  • Retrieve the base path dynamically using \Drupal::request()->getBasePath() and store it in a variable.
  • Prepend the base path to href values (if missing) during validation to ensure consistency across environments.
joseph.olstad’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.