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

  1. Install a new Drupal site with the Standard profile.
  2. Go to /admin/structure/menu/manage/main/add
  3. 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.
  4. Edit the menu link that you just created. Change the title. Note that the path is now view.frontpage.page_1 instead of route:view.frontpage.page_1. Attempt to save. Note a validation error: Manually entered paths should start with one of the following characters: / ? #

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.

Issue fork drupal-3115188

Command icon 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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

angelamnr created an issue. See original summary.

sja112’s picture

Assigned: Unassigned » sja112
sja112’s picture

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

mrinalini9’s picture

Assigned: sja112 » mrinalini9
mrinalini9’s picture

Hi,

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

mrinalini9’s picture

Assigned: mrinalini9 » Unassigned
sja112’s picture

Priority: Minor » Normal

Changing issue priority.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

darvanen’s picture

Issue tags: +Bug Smash Initiative

Closed #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.

benjifisher’s picture

Usability 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. Then

  • Show the additional help text when the new option is enabled.
  • Make the new option disabled by default for new fields.
  • Make the new option enabled for existing fields, to avoid breaking existing sites.

With or without the new option, it might work well to rewrite the help text using an unordered list for the different kinds of links.

benjifisher’s picture

Issue summary: View changes

I 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.

darvanen’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

I 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.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

heddn’s picture

Another issue with the current getUriAsDisplayableString approach is that route:entity.group_content.canonical;group_content=135&group=3 gets truncated to ntity.group_content.canonical;group_content=135&group=3. That is an outright bug.

heddn’s picture

Here we add some test coverage and rebase this as a patch on 10.1.x.

heddn’s picture

Why 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.

Status: Needs review » Needs work

The last submitted patch, 20: 3115188-20.patch, failed testing. View results

heddn’s picture

Status: Needs work » Needs review

Random test failure.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests +Needs Review Queue Initiative

Ran 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.

larowlan’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record
+++ b/core/modules/link/tests/src/Functional/LinkFieldTest.php
@@ -160,11 +162,6 @@ protected function doTestURLValidation() {
-      // Text-only links.
-      '<nolink>' => '&lt;nolink&gt;',
-      'route:<nolink>' => '&lt;nolink&gt;',
-      '<none>' => '&lt;none&gt;',

why 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:

matthieuscarset’s picture

Removing getUriAsDisplayableString makes sense.

It fixes the situation for me too.

+1 for patch in #20.

mably’s picture

Just 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 🙏 😉

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

heddn’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record
FileSize
1.8 KB
1.18 KB

re #25:

See #21. More specifically, testNoLinkUri. Since doTestURLValidation has 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.

Status: Needs review » Needs work

The last submitted patch, 29: 3115188-29-test-only.patch, failed testing. View results

Ajeet Tiwari’s picture

Status: Needs work » Needs review
FileSize
3.16 KB

Please review.

Status: Needs review » Needs work

The last submitted patch, 31: 3115188-31.patch, failed testing. View results