Problem/Motivation

With standard pathauto we can get the aliased path with getAliasByPath. This doesn't work if we have a subpath.

Steps to reproduce

Take a standard node: node/12345 aliased as "mynodealias"
Alter its path to add a subpath "mysubpath" with alterRoutes (inside "class RouteSubscriber extends RouteSubscriberBase") or any other way
Visit "node/12345" which will redirect to "mynodealias"
Test in node_view_alter:
$request = \Drupal::request();
$uri = $request->getRequestUri();
dpm($uri, 'uri'); will print "/mynodealias"
$current_path = \Drupal::service('path.current')->getPath();
dpm($current_path, 'current path'); will print "/node/12345"
$alias= \Drupal::service('path_alias.manager')->getAliasByPath($current_path);
dpm($alias, 'alias'); will print "/mynodealias" -> AS EXPECTED

Visit "node/12345/mysubpath" which will redirect to "mynodealias/mysubpath"
Test:
$request = \Drupal::request();
$uri = $request->getRequestUri();
dpm($uri, 'uri'); will print "/mynodealias/mysubpath"
$current_path = \Drupal::service('path.current')->getPath();
dpm($current_path, 'current path'); will print "/node/12345"
$alias= \Drupal::service('path_alias.manager')->getAliasByPath($current_path);
dpm($alias, 'alias'); will print "/node/12345/mysubpath" -> EXPECTED "/mynodealias/mysubpath"

You can verify the same with user's contact form
uri => "/users/name/contact"
current path => "/user/1234/contact"
alias (with getAliasByPath) => "/user/1234/contact" INSTEAD OF "/users/name/contact"

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Command icon 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

erwangel created an issue. See original summary.

batigolix made their first commit to this issue’s fork.

batigolix’s picture

Version: 8.x-1.3 » 8.x-1.x-dev
Category: Feature request » Bug report
Issue tags: +finalist-sprint

batigolix’s picture

Title: Make Sub-pathauto compatible with getAliasByPath » Make Sub-pathauto compatible with getAliasByPath / Fix SubPathautoKernelTest::testProcessOutbound
Priority: Normal » Major

[deleted]

batigolix’s picture

Title: Make Sub-pathauto compatible with getAliasByPath / Fix SubPathautoKernelTest::testProcessOutbound » Make Sub-pathauto compatible with getAliasByPath
batigolix’s picture

Priority: Major » Normal
batigolix’s picture

With some help, I created a first draft:

Approach: decorator on path_alias.manager

Drupal core's AliasManager::getAliasByPath() only performs direct alias lookups. For /node/1/edit, with an alias /node/1/kittens, it returns /node/1/edit unchanged because no alias exists for the full path.

MR !11 adds a decorator that extends getAliasByPath() with sub-path traversal:

  1. Direct lookup first — if the inner manager already finds an alias for the full path, return it as-is.
  2. Otherwise, sub-path traversal — strip segments from the right one by one, ask the inner manager for the shorter path. On a hit, return the alias with the stripped segments re-appended.

The change already assumes that the ignore_list option is used which lands in #2964786: Ignore specific paths.

batigolix’s picture

Status: Active » Needs review