Problem/Motivation

#1543750: Allow menu items without path wanted non-link menu link items but we can do better and solve it on a link level.

Proposed resolution

Add a <nolink> route which renders a <span> instead of an <a> in linkGenerator::generate.

Remaining tasks

User interface changes

API changes

If someone defined a <nolink> route that breaks... but why would you define such a thing? And how could we a route ever if that's a problem.

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

chx created an issue. See original summary.

chx’s picture

chx’s picture

dawehner’s picture

+++ b/core/lib/Drupal/Core/Utility/LinkGenerator.php
@@ -147,16 +147,21 @@ public function generate($text, Url $url) {
+      $generated_link = new GeneratedLink();

If we do that, it would be nice to mention that on GeneratedLink itself

star-szr’s picture

From a frontend POV <span> is probably the right thing to use here. I'm curious what kind of attributes would/could end up on that element though (in common scenarios). The test just shows a span with no attributes.

Status: Needs review » Needs work

The last submitted patch, 2: 2693725_2.patch, failed testing.

chx’s picture

Title: <none> is not linkgenerated properly » Add <text> to allow for non-link links
Category: Bug report » Task
Issue summary: View changes
Status: Needs work » Needs review
FileSize
3.72 KB
4.14 KB
chx’s picture

Issue summary: View changes

Status: Needs review » Needs work

The last submitted patch, 7: 2693725_7.patch, failed testing.

chx’s picture

Issue summary: View changes
Status: Needs work » Needs review
FileSize
4.17 KB
dawehner’s picture

Title: Add <text> to allow for non-link links » Add <nolink> to allow for non-link links
star-szr’s picture

Status: Needs review » Needs work

A few of us talked about this on IRC and I think we agreed that ideally we would just use <none> but it's already taken. The nice thing about <nolink> IMO is that it's theoretically in use by almost 100k Drupal 7 sites already: https://www.drupal.org/project/special_menu_items

A more ideal solution is probably to (also) make this part of the menu link UI somehow but that's definitely out of scope here.

+++ b/core/lib/Drupal/Core/GeneratedNoLink.php
@@ -0,0 +1,15 @@
+ * Contains \Drupal\Core\GeneratedTextLink.
...
+class GeneratedNoLink extends GeneratedLink {

@file docblock is slightly out of date.

chx’s picture

Status: Needs work » Needs review
FileSize
4.17 KB

docblock fixed.

dawehner’s picture

Ah I was gonna say we should add test coverage for using that in the UI< but I totally believe that this will actually happen as part of #1543750: Allow menu items without path

+++ b/core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php
@@ -152,6 +153,26 @@ public function testGenerate() {
+    $this->assertTrue($result instanceof GeneratedNoLink);

Note: you could use assertInstanceOf if you like.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Sorry, I actually wanted to RTBC it.

Eric_A’s picture

Is changing the tag from a to span in scope for this issue? An a tag without href attribute is perfectly valid HTML5, no?

dawehner’s picture

If you theme that, do you think this would work as well?

Eric_A’s picture

If you theme that, do you think this would work as well?

Sorry, not sure what this means...

Anyways, the a tag use case would probably be better served by something like <placeholderlink> and the <nolink> route seems to suit the span markup.
It feels natural to address the simple placeholder hyperlink use case first...

Wim Leers’s picture

Status: Reviewed & tested by the community » Needs work

Shouldn't this also have a test for UrlGenerator?

chx’s picture

Perhaps. It won't be me, though.

dawehner’s picture

@Wim Leers
What kind of test do you have in mind? The YML definition is pretty clear, this is tested thousands already for <none>

Wim Leers’s picture

Status: Needs work » Reviewed & tested by the community

It just seems like all special cases should be tested explicitly. But I trust @dawehner's judgment: apparently this is less special than I thought.

xjm’s picture

Version: 8.1.x-dev » 8.2.x-dev

Discussed with @Cottser, @effulgentsia, and @alexpott. This one should be targeted for 8.2.x now for the addition like #1543750: Allow menu items without path which it unblocks. (This means it can be committed at any time rather than being subject to the RC freeze.)

Thanks everyone!

alexpott’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
2.21 KB
4.6 KB

Discussed with @dawehner - the LinkGenerator shouldn't have to know anything about which tags to use. Whilst the solution in the attached patch is not perfect - moving the HTML generation to the value object is out of scope here.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Small improvement, still not ideal of course, but well, let's see where we can get in 8.2.x

  • catch committed a39876b on 8.2.x
    Issue #2693725 by chx, alexpott: Add <nolink> to allow for non-link...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.2.x, thanks!

Wim Leers’s picture

  1. +++ b/core/lib/Drupal/Core/GeneratedNoLink.php
    @@ -0,0 +1,15 @@
    +class GeneratedNoLink extends GeneratedLink {
    +
    +  /**
    +   * {@inheritdoc}
    +   */
    +  const TAG = 'span';
    +
    +}
    

    This must be one of the funniest & funkiest subclasses in all of Drupal :)

  2. +++ b/core/lib/Drupal/Core/Utility/LinkGenerator.php
    @@ -142,7 +143,7 @@ public function generate($text, Url $url) {
    +    // them. Make sure the "href" comes first for testing purposes.
    

    "href first" -> ugh, just use actual HTML parsing rather than string parsing… But this is a pre-existing thing, this is just clarifying the comment.

andypost’s picture

Looks this needs changes record!

dawehner’s picture

rcodina’s picture

Any workaround to do this on Drupal 8.1.1?

Status: Fixed » Closed (fixed)

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

risforrocket’s picture

I tried as the link and I still get the error message "Manually entered paths should start with /, ? or #." in 8.2.x

jibran’s picture

ivriets’s picture

ivriets’s picture

for 8.2.x use route:<nolink>

StG’s picture

The route:<nolink> works more or less, but a major issue has been still present: https://www.drupal.org/node/2838351 (I use 8.2.6)

EDIT

I found a solution (or a workaround) for this issue: If I set the parent menu item (with route:<nolink> path) to Show as expanded, it works as expected.

platinum1’s picture

Now that the special_menu_items module is no longer supported, I tried route: on the latest 8.4.dev

I get the error message "Manually entered paths should start with /, ? or #." as well. Is there a different strategy for 8.4.dev?

brianwagner’s picture

Seems to be some confusion here, me included:

You must type

route: <nolink>

in the form field, including the word 'route' and a colon.

Screenshot of form field: "route nolink"

blainelang’s picture

Thanks! that works. Maybe it should be added to the field help text as one of the examples.

platinum1’s picture

Post #39 shows a space between "route:" and "".
IF someone copy & pastes this, the route is rejected, due to the space.

hass’s picture

This is all very bad usability.