Problem/Motivation
You can add routes to a link field by using route:{$route_name}. However, if you save an entity with a link field that has a route and attempt to edit and re-save the node or menu item without changing the route in the link field, the route will fail validation.
Steps to reproduce
- Install a new Drupal site with the Standard profile.
- Go to
/admin/structure/menu/manage/main/add - Add a menu link to the frontpage view using its route. Enter anything for the title. The path should be
route:view.frontpage.page_1. Save your link. - Edit the menu link that you just created. Change the title. Note that the path is now
view.frontpage.page_1instead ofroute:view.frontpage.page_1. Attempt to save. Note a validation error:Manually entered paths should start with one of the following characters: / ? #
Proposed resolution
It looks like the code that makes routes fail validation was added to handle display of route:<nolink>:
https://github.com/drupal/drupal/blob/8.9.x/core/modules/link/src/Plugin...
However, route:<nolink> is handled later in the link widget code and other routes are not:
https://github.com/drupal/drupal/blob/8.9.x/core/modules/link/src/Plugin...
Luckily, there is an easy workaround for this bug. Each time you edit an entity with a link field that has a route, simply re-add the route: portion to your link.
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|
Issue fork drupal-3115188
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:
- 3115188-d11
changes, plain diff MR !13300
- 11.x
compare
- 3115188-routes-fail-validation
changes, plain diff MR !1590
Comments
Comment #2
sja112 commentedComment #3
sja112 commentedCreated a patch to fix the issue.
Comment #5
mrinalini9 commentedComment #6
mrinalini9 commentedHi,
Reviewed patch #3 and it applied successfully for the core module Link on drupal versions: 8.9.x-dev and 9.0.0-beta2. And also works fine as now able to edit the title and saved it successfully instead of getting validation error as mentioned above.
Thanks & Regards
Mrinalini
Comment #7
mrinalini9 commentedComment #8
sja112 commentedChanging issue priority.
Comment #12
darvanenClosed #3209656: LinkWidget::getUriAsDisplayableString() can not handle route: URIs as duplicate of this issue.
It's unclear right now whether
route:schemas are intended to be supported by the link field. The method used by the link field to generate URLs can easily handle routes, however routes are generally a developer tool and not well understood by content editors. I think we need some guidance, maybe from the UX team, regarding whether it should be possible to link to routes in all link widgets.If the answer is no, then we need some more validation to rule out
route:schemas (bar the exceptions).If the answer is yes, then we need to discuss a way forward. Personally I think leaving the schema visible when displaying the value is both the easiest approach and the most understandable to the end user.
Comment #13
benjifisherUsability review
We discussed this issue at #3253969: Drupal Usability Meeting 2021-12-17. That issue has a link to a recording of the meeting.
One consideration overrides usability concerns: we cannot make changes that break existing sites. If there are sites that currently have
route:...in some of their link fields, then we cannot invalidate them. I think we cannot take away the option of editing them, either.If we are going to keep the feature, then we should document it. Unfortunately, that means adding some pretty technical information to the help text (description).
Looking at the configuration of a link field, I see that there are already several options: internal/external/either; link text required/allowed/forbidden. We could add another option: allow
route:...for the link. ThenWith or without the new option, it might work well to rewrite the help text using an unordered list for the different kinds of links.
Comment #14
benjifisherI forgot to mention: once we officially support the option, we should support it better. That means do not strip the "route:" prefix when creating the default value for the form field.
In other words, +1 to the last paragraph of #12.
Comment #16
darvanenI haven't tried it out properly but this approach (in the MR) should deal with the bug. I haven't started on any of the extra parts outlined in #13, just giving it a kick-start.
Comment #19
heddnAnother issue with the current
getUriAsDisplayableStringapproach is thatroute:entity.group_content.canonical;group_content=135&group=3gets truncated tontity.group_content.canonical;group_content=135&group=3. That is an outright bug.Comment #20
heddnHere we add some test coverage and rebase this as a patch on 10.1.x.
Comment #21
heddnWhy did we remove the
Text-only links.test cases? Because there is dedicated test coverage, it wasn't needed and the regex parsing was causing errors on those test cases.Comment #23
heddnRandom test failure.
Comment #24
smustgrave commentedRan the test locally without the fix and it failed as expected
Behat\Mink\Exception\ExpectationException : The string ""route:entity.node.canonical;node=1"" was not found anywhere in the HTML response of the current page.
So removing the tag.
The change in question seems good though.
Comment #25
larowlanwhy are we removing these?
Can we also get a red/green set of patches here demonstrating the new test failures?
Thanks
I think we also want a change notice for this detailing the official support for route:
Comment #26
matthieuscarset commentedRemoving
getUriAsDisplayableStringmakes sense.It fixes the situation for me too.
+1 for patch in #20.
Comment #27
mably commentedJust been hit by the problem today.
Spent a bunch of time to understand what was going on and finally found this issue.
Hoping to have it fixed one day 🙏 😉
Comment #29
heddnre #25:
See #21. More specifically,
testNoLinkUri. SincedoTestURLValidationhas duplicate coverage that is difficult to handle with the way that regex works, I removed it as opposed to adding one-off logic for it.Added a CR and uploading a test-only patch. The full patch is a duplicate of #20.
Comment #31
Ajeet Tiwari commentedPlease review.
Comment #37
prudloff commentedI created a new MR based on 11.x but unit tests are failing.
Comment #38
dcam commentedLinkFieldTestfailed on the "Text-only links" section ofdoTestURLValidation(). The code to remove "route:" from the links before displaying them in the widget was removed. This doesn't only apply to routes like views. It also applies to the special routes. Lines 160-164 ofLinkFieldTestwould need to be updated like this:To be honest, I don't like this very much. I don't think "route:" should appear before these links. It may be confusing to users. In my opinion we should continue to strip "route:" from them.
Comment #39
prudloff commentedI added a condition for
<nolink>,<none>and<button>because these routes have a special handling in LinkWidget::getUserEnteredStringAsUri().Comment #40
dcam commented@prudloff thank you for working on this. I left a comment on the MR due to some code duplication.
Comment #41
dcam commentedI added my own suggestion that was mentioned in #40.
Comment #42
smustgrave commentedTested following the steps in the IS
On a standard install I went and added a link.
Title: Test
URL: route:view.frontpage.page_1
It saved fine = EXPECTED
When I edited I just see view.frontpage.page_1 = BUG
Changed the title to "Test2"
I see the error = BUG
Applied the MR
I was happy to see I didn't have to change anything or save it just fixed itself
I see route:view.frontpage.page_1 and can save.
https://git.drupalcode.org/issue/drupal-3115188/-/jobs/7801499 shows the test coverage
Summary appears clear to me.
LGTM
Comment #46
catchCommitted/pushed to main and 11.x, thanks!
Apologies to @dcam who I somehow left off the commit message, but did credit on the contribution record.