Unify paths to start with a leading slash (/) in pattern

Created on 6 August 2024, 4 months ago
Updated 17 September 2024, 3 months ago

Problem/Motivation

Drupal Core since Drupal 8 uses a leading slash in path fields, e.g. Node path alias, custom menu items, ...

Some modules, especially coming from Drupal 7 break with the new standard.
Sadly Pathauto as a very widely used module also breaks with it in the path pattern.

This should be fixed to validate the path pattern to start with a leadings slash.

Steps to reproduce

Enter a path pattern without a leading slash, e.g. faq/[node:title].
The form can be saved without an error and the value is also saved and displayed without a leading slash.

Adding a leading slash also works, which makes it even unclear what's expected and which side-effects this might have!

Proposed resolution

  1. Add the core description to the "Path pattern" input field
  2. Add a validation for the leading slash, like for other path fields in core. If the leading slash is missing, reject the submission. The core error message is: "The alias path has to start with a slash."
  3. Add an update hook to transform all path patterns to use the leading slash. Ensure that this won't result in double leading slashes, by ltrim('/')'ing the value first
  4. Ensure the migration also adds a leading slash when upgrading from earlier Drupal versions

See the implementations here: https://git.drupalcode.org/project/drupal/-/blob/11.x/core/modules/path_...

As a common module, pathauto should act as role model for other modules here.

Remaining tasks

User interface changes

API changes

Data model changes

๐Ÿ› Bug report
Status

Needs work

Version

1.0

Component

Code

Created by

๐Ÿ‡ฉ๐Ÿ‡ชGermany Anybody Porta Westfalica

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

Merge Requests

Comments & Activities

  • Issue created by @Anybody
  • Assigned to sourav_paul
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia sourav_paul Kolkata
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany Anybody Porta Westfalica
  • Merge request !85Initial implementation โ†’ (Open) created by Anybody
  • Pipeline finished with Failed
    4 months ago
    Total: 159s
    #245645
  • Pipeline finished with Failed
    4 months ago
    Total: 161s
    #245762
  • Issue was unassigned.
  • Status changed to Needs review 4 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia sourav_paul Kolkata
  • Status changed to Needs work 4 months ago
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany Anybody Porta Westfalica

    @Sourav_Paul tests still fail and there's a lot of work to do. Changing a word isn't a big deal here, sorry. But thank you anyway.

  • Assigned to lrwebks
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany lrwebks Porta Westfalica
  • Pipeline finished with Failed
    3 months ago
    Total: 173s
    #285099
  • Pipeline finished with Success
    3 months ago
    Total: 177s
    #285109
  • Status changed to Needs review 3 months ago
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany lrwebks Porta Westfalica
  • Pipeline finished with Failed
    3 months ago
    Total: 286s
    #285124
  • Status changed to Needs work 3 months ago
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany Anybody Porta Westfalica

    @lrwebks thanks! Some points:
    1. Pipeline is red, while it's green on the project page, that looks wrong?
    2. Shouldn't the value be validated in the form?

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany lrwebks Porta Westfalica

    @anybody regarding #9

    1. I'm looking into it right now
    2. The function pathauto_pattern_validate() from the pathauto.module file is called from the form via the following line in PatternEditForm.php:
      '#element_validate' => ['token_element_validate', 'pathauto_pattern_validate'],
      

      The other checks for missing tokens and whitespace at the end of the path were already placed in the pathauto_pattern_validate() function, which is why I placed the new check there as well. I suppose that we could try to move this whole functionality over to the form if it is not used by any other part of the module, but the current implementation works fine right now.

    3. Also, are you able to resolve the two threads that I opened? Resolving your own threads seems to be disabled in the GitLab UI for this module.
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany lrwebks Porta Westfalica

    Well, the failing test was green locally, so I decided to try to run another pipeline, and: No more fails! Seems weird to me though, as I did not update the fork or anything similar in between pipelines, so this feels like a weird PHPUnit hiccupโ€ฆ It could be related to the fact that the failed FunctionalJavascript test seems to use the waitForElementVisible() function a lot, which can fail randomly if the page loading takes too long.
    Anyway, back to review!

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany lrwebks Porta Westfalica
  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland berdir Switzerland

    Adding a leading slash also works, which makes it even unclear what's expected and which side-effects this might have!

    The answer should be "no side effects", generated aliases will always have a leading /.

    Instead of failing with an error, we could just ensure the / is always there and add it on save?

    Also not sure this is a bug, this just makes things a bit more consistent in the pathauto UI.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany Anybody Porta Westfalica

    Thanks for the feedback @berdir. :)

    Also not sure this is a bug, this just makes things a bit more consistent in the pathauto UI.

    Yes, I remember Drupal 8 made this switch in several cases in core, forcing to start with a leading slash, which was unclear in Drupal 7- And I think it's a good idea for consistency = usability. So I'd clearly vote to do the same in all Drupal 8+ modules, even if it's not a clear bug.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany Grevil

    I agree with @berdir here. Instead of throwing a validation error, we should simply add a leading slash to the pathauto pattern on submit, if none exists, instead of throwing a validation error here!

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States dalemoore

    I'm in favor of consistently including the leading slash, I just noticed that in this new site I am working on I've included it in some patterns and excluded it in others. As long as it silently adds it on save but continues to work for existing patterns without (or maybe updates existing ones on sav of the Patterns page, or in a module update hook) it sounds like a nice feature to improve consistency.

Production build 0.71.5 2024