Problem/Motivation
An anchor added to URL path in link fields is saved initially, but when you edit the entity again, it gets lost.
Steps to reproduce
- Edit a node with a link field that's configured to use the Linkit widget.
- Use the autocomplete funcationality, or enter an alias path manually to select a link target.
- Add a URL fragment to the path, e.g. #foo.
- Save the node. In the front-end, the link will contain the fragment.
- Edit the node again. The URI field gets prefilled with the linked entity's alias path, but without the fragment.
- Save the node again. In the front-end, the link will NOT contain the fragment any more.
The same might happen to a manually added query string.
I see lots of work has been done for a similar task in #3022261: Add better support for linking to anchors, but for the filter, not the widget.
Proposed resolution
Append fragment (and query string) to the entity's URL alias when filling the URI field.
Remaining tasks
User interface changes
API changes
Data model changes
Comments
Comment #2
mrshowermanInitial patch that brings back fragment and query to the link field value.
Comment #3
mark_fullmerThanks much! I'm adding this to my "review in the next week" list :)
Comment #4
mark_fullmerComment #5
mark_fullmerThe patch in #2 didn't seem to handle query parameters correctly. Per Drupal's
Urldocblock comments, it needs to be an array:I modeled the patch revision on Drupal core's method for preparing query parameters.
There was test coverage for the initial rendering of the link with an anchor, but no test coverage for revisiting the edit form, which is what this issue is related to. I've added test coverage for that.
Comment #6
mrshowermanAwesome! Works like a charm. I have to admit that my main focus was the fragment part. With the new patch, query strings work as well now 👍.
Comment #9
mark_fullmerThanks for the review!
Committed to 6.1.x (Drupal 10.1.x) and cherry-picked for 6.0.x (Drupal 10.0.x).