2

In Drupal 8, what code determines if a link is output as text, or an anchor tag with text?

I have entered a route:<nolink> item in the menu, but the output is this:

<span target="" data-highlight="0" data-icon="" title="Description of wris." data-drupal-link-system-path="<front>">wris</span>

Instead, I want an anchor tag with just # for the href.

Is there a reason why it only outputs a span? Where can I adjust this? Or, should I do this?

6

TL:DR; you need to overwrite the link_generator to change this behavior.

Drupal 8 uses the link generator for this. You can take a look at

Drupal\Core\Utility\LinkGenerator::generate if you want to know the code behind it all.

The key part is this:

elseif ($url->isRouted() && $url->getRouteName() === '<nolink>') {
  $generated_link = new GeneratedNoLink();
  unset($attributes['href']);
}

If you take a look at GeneratedNoLink you will see

/**
 * This class holds a <span> generated from the <nolink> route.
 */
class GeneratedNoLink extends GeneratedLink {

  /**
   * {@inheritdoc}
   */
  const TAG = 'span';

}

So you need to implement your own no link class and not unset the href and everything should be fine. Doing so you more or less need to copy most of the logic in Drupal\Core\Utility\LinkGenerator::generate to determine when no link should be used.

Note

Links/urls can be generated in various ways, some use link_generator, some use url_generator, some use the link RenderElement. In your case it looks like the link_generator is used, but that could be changed by custom theme/module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.