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

  1. Edit a node with a link field that's configured to use the Linkit widget.
  2. Use the autocomplete funcationality, or enter an alias path manually to select a link target.
  3. Add a URL fragment to the path, e.g. #foo.
  4. Save the node. In the front-end, the link will contain the fragment.
  5. Edit the node again. The URI field gets prefilled with the linked entity's alias path, but without the fragment.
  6. 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

mrshowerman created an issue. See original summary.

mrshowerman’s picture

Assigned: mrshowerman » Unassigned
Status: Active » Needs review
StatusFileSize
new921 bytes

Initial patch that brings back fragment and query to the link field value.

mark_fullmer’s picture

Thanks much! I'm adding this to my "review in the next week" list :)

mark_fullmer’s picture

Title: Anchors in link fields get lost after save » Query fragments or anchors on internal URLs in link fields get lost on re-editing
mark_fullmer’s picture

StatusFileSize
new3.24 KB

The patch in #2 didn't seem to handle query parameters correctly. Per Drupal's Url docblock comments, it needs to be an array:

  *   - 'query': An array of query key/value-pairs (without any URL-encoding)
   *     to append to the URL.

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.

mrshowerman’s picture

Awesome! 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 👍.

  • mark_fullmer committed 002b8aee on 6.1.x
    Issue #3359375 by mrshowerman, mark_fullmer: Query fragments or anchors...

  • mark_fullmer committed 300606a4 on 6.0.x
    Issue #3359375 by mrshowerman, mark_fullmer: Query fragments or anchors...
mark_fullmer’s picture

Status: Needs review » Fixed

Thanks for the review!

Committed to 6.1.x (Drupal 10.1.x) and cherry-picked for 6.0.x (Drupal 10.0.x).

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.