Problem/Motivation

For an project we are using the path alias pattern: [node:menu-link:parents:join-path]/[node:menu-link:title]
This works really well, but we would like the option to remove the root item from the generated join-path.

Steps to reproduce

See pattern above.

Proposed resolution

A patch providing the new token:

  • [node:menu-link:parents:join-path-noroot]

Remaining tasks

Create patch for this feature.

Issue fork pathauto-3238920

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

sam-tripp created an issue. See original summary.

sam-tripp’s picture

joseph.olstad’s picture

Status: Active » Needs work
Issue tags: +Needs tests

Great patch, confirmed that it works as intended! I've come across this use case before and wanted this functionality so great job! I think it's a worthy patch and looks perfect except just needs tests.

Needs tests ported from join-path tests so setting the issue to 'Needs Work'.

berdir’s picture

This would make more sense to me on the parents level aka parents-no-root, join-paths is an array-token and doesn't really have a meaning about root. If anything, it would be no-first then or something.

That also avoids the safe tokens problems because that part of the token is unchanged.

joseph.olstad’s picture

Good point @Berdir , yes parents-no-root I agree.

we should look into making that change.

joseph.olstad’s picture

ok, ya parents-no-first is also a good suggestion for a name

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

mably’s picture

Tried another solution by upgrading behavior of join-path token:

  • join-path — works as before (all elements)
  • join-path:1 — skip first element (e.g., skip root parent)
  • join-path:2 — skip first two
  • join-path:-1 — last element only
  • join-path:0:2 — first two elements only
  • join-path:1:1 — second element only
mably’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests

Some tests were added.

mably’s picture

Assigned: sam-tripp » berdir

Summary

Added optional array slice syntax to the join-path token, allowing users to exclude elements (e.g. root menu item) from generated path aliases.

Problem

Users building menu-based aliases with [node:menu-link:parents:join-path] had no way to exclude the root menu item from the generated path. The only option was to include all parent elements or none, which forced workarounds like manually removing the root item from the "Strings to remove" settings.

Fix

  • PathautoTokensHooks.php: Replaced the fixed switch/case 'join-path' with a regex match that accepts optional offset and length parameters using PHP's array_slice() semantics. The supported syntax is:
    • join-path — all elements (backward compatible, unchanged behavior)
    • join-path:1 — skip the first element (the original request)
    • join-path:2 — skip the first two elements
    • join-path:-1 — last element only
    • join-path:0:2 — first two elements only
    • join-path:1:1 — second element only
  • PathautoTokenTest.php: Added five test cases covering skip-first, skip-two, negative offset, offset with length, and single-element extraction.