Problem/Motivation
URL ending forward slash(‘/‘) breaks url alias(pretty url) paths.
Admin area where users are able to make menu edit changes to menu links e.g. /admin/structure/menu/manage/main seem to break core functionality when appending the forward slash to node references. The following steps will recreate the issue.
Steps to recreate:
1. Visit admin/config/search/path
2. Click on the ‘edit’ operation, assuming there are existing menu links created.
3. Append the path field value with the forward slash character ’/‘. e.g. /node/1 will become /node/1/
4. Save the changes.
5. Visit any page that contains the main menu and you’ll notice the url alias no longer working. The link will now reference the node path with the ‘/‘ prepended.
Proposed resolution
Create a post-update hook in the path_alias module to execute scripts that update aliases in batches, removing trailing spaces and slashes.
| Comment | File | Size | Author |
|---|---|---|---|
| #42 | interdiff-35-42.txt | 1.56 KB | hardik_patel_12 |
| #42 | 3145132-42.patch | 3.31 KB | hardik_patel_12 |
| #33 | 3145132-33.patch | 2.49 KB | kapilv |
| #32 | interdiff-3145132-27-32.txt | 665 bytes | mohit_aghera |
| #32 | 3145132-32.patch | 2.49 KB | mohit_aghera |
Issue fork drupal-3145132
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
Comment #2
cm0dit commentedComment #4
ankitsingh0188Comment #5
cm0dit commentedMy apologies, while testing, I forgot to uncomment and important piece of the patch!
Comment #6
ankitsingh0188Hi @cm0dit,
Please make sure that the issue is assigned to someone else or not before adding a patch or comment. As I am also working on test case failures, but I just saw your comments.
Anyways, please run the test cases for the #5 patch.
Comment #7
hardik_patel_12 commentedworking on it.
Comment #8
ankitsingh0188Hi @Hardik_Patel_12,
I have assigned this ticket to myself and you have changed the assignee and assigned to yourself. I think this is not the right approach. Please make sure that the ticket is not assigned to someone before working on that.
Thanks!
Comment #9
hardik_patel_12 commentedKindly review a new patch.
Comment #10
hardik_patel_12 commentedHi @ankit.singh , ticket was assign to you 4 days ago so that's why i thought you are not working on this. At least you should write that i am working on this issue so then only people will understand that not to pick this issue. So its a kindly request that if we are facing some trouble in the issues and spending couple of days then simply unassign the issue so other people can work on it.
Thanks!
Comment #11
ankitsingh01881. Visit admin/config/search/path
2. Append the System path field value
'/'Before Patch: It shows a 404 page not found an error.
After Patch: Patch applied successfully and the respective entity page opens up.
Good to move it to RTBC +1
Comment #13
ankitsingh0188I have re-run the #9 patch for Drupal version 8.9 and the patch applied successfully.
Comment #14
hardik_patel_12 commentedThanks @ankit.singh , for verifying now its looks good to go. Moving to RTBC.
Comment #15
xjmThanks everyone for filing this bug report and for fixing the issue. Bug fixing is very valuable.
@ankit.singh, thank you for looking into to this issue. Posting screenshots of your codebase or CLI does not advance the issue, since the automated testing infrastructure tells us whether the patch applies correctly. However, the before-and-after screenshots of the UI are potentially helpful, so leaving credit for that. Next time, could you crop them only to the relevant part of the screen, and embed them in the issue?
So, I've removed the issue credit for that screenshot. In the future, you can get credit for issues by reading the issue to understand its purpose, and posting your review or testing of that purpose, including whether it completely fixes the issue, whether it is the best fix, and so on. Thank you!
The comments here are not wrapping properly. We need to make sure the new comment is wrapped at 80 characters.
In order to commit a bug fix, we need an automated to test to prove that we've fixed the bug and ensure that we don't break it again in the future. For more information about writing tests in Drupal 8 see the following links:
Looks like this is a difficulty between the
setAlias()andsetPath()methods onPathAlias.I checked their relative docs on the interface.
setAlias()has:...That's not helpful to our situation, where we are trying to decide if this is an appropriate way to mess around with
PathAlias::preSave(). However, it does sound like the trimming is supposed to be done on output, notinput. So I'm not totally sure of changing the user's input here.Comment #16
hardik_patel_12 commentedcovering points suggested by @xjm .
#15.1 done
#15.2 adding test case for this bug
#15.3 i think changing the user's input here its ok , but yes i am also still not sure about this.
Comment #17
hardik_patel_12 commentedComment #18
jungleWrapped too early.
Should be
{@inheritdoc}Wrapped too early
t()is unnecessaryWrapped too early.
getPid()?What if it's
[]Comment #19
naresh_bavaskarComment #20
naresh_bavaskarAdded test cases (Kernal).
@xjm Thanks for the suggestion. I think trimming supposed to do on input (not output) othewise every request It needs to trim (performance)
Please review
Comment #21
jungleWell, #20 is a kernel test. not a funcational test as #16. So uploading a test only patch based on #20.
(Yeah, It's bad that the last patch is a test-only patch)
Comment #24
jeroentComment #25
berdirthis somehow changed from "to not" to "do not", that's not correct grammar, was right before.
Comment #26
naresh_bavaskar@berdir, Thanks for focusing it out. I think it mistakenly added by me. will correct it
Comment #27
naresh_bavaskarFixed grammatical mistake in comment #20.
Comment #28
hardik_patel_12 commentedNow patch is looks good to me , grammar change is also covered as suggested by @Berdir , moving to RTBC.
Comment #29
catchDoes this need an update to trim trailing forward slashes from existing data?
Comment #30
amateescu commentedYup, I think we need an update function.
Comment #31
catchOK let's add that here.
Comment #32
mohit_aghera commented- Adding an update hook to re-save all the path aliases.
I am wondering if we need more validation around it to save only paths which ends with "/"
As, I'm just re-saving nodes, I haven't added tests for update hook.
@catch, please suggest if we want to refactor the logic to update existing path aliases.
Comment #33
kapilv commentedFixed Custom Commands Failed.
Comment #34
catchThis could be done in a hook_post_update_NAME(), since it's affecting content rather than schema. Also needs to use batch since there could be millions of aliases on a site.
Comment #35
ankithashettyUpdated the patch in #33 addressing the changes suggested in #34, i.e., moved the update_9100() hook in
path_alias.installto post_update_Name hook inpath_alias.post_update.php.Also tried to use the batch process within the post_update_Name hook, please review.
Thank you!
Comment #39
smustgrave commentedtriggering for 10.1
Comment #40
smustgrave commentedTested this on Drupal 10.1 with a standard install
Followed the steps in the issue summary and confirmed I was able to add a URL alias /node/1/
With node/1 having an alias of /hello-world.
I add a menu link to the main navigation for node/1
When I got to any page I see that the alias works fine.
Unless I missed a step
But agree it should cleanup the trialing /
Can the issue summary be updated with the proposed solution.
Thanks.
Comment #42
hardik_patel_12 commentedAdded type hinting in the batch process and changed the limit from 25 to 50 in the post_update hook.
Comment #43
hardik_patel_12 commentedI've revised the issue description; please feel free to suggest any further changes if you think they are needed.