3

I have a clean Drupal 8 install with bootstrap theme. I just want to add a simple link to the registration form in the user account menu and to it only be visible if you are not logged in.

The login and my account links are already standard in, why isnt the register button there? What would be the solution to this?

Creating the link normally in the menu -> add link option makes it visible all the time.

1

3 Answers 3

2

I haven't been able to find a contributed module that might make this easier, so the only other possibility is code.

What you are basically asking for is the altering of an existing route, that was defined by the User module. I'm not sure how much experience you have with the new Drupal 8 architecture and APIs, so I'm going to just dump a few links here :p

The first is the documentation of the new routing system that Drupal 8 is using. The route that you want to change is the one with the path /user/register, so you will have to look for the route's name and alter it.

A good article about how to do that can be found here. Your use-case is very similar to what the example shows with the logout route.

So basically you will have to write a RouteSubscriber that will alter your route, based on the current user's loggedIn status. I'm not at my PC at the moment, so I can't give code example, but if you need, then I can share a solution.

Be advised, that there are lots of new concepts that you will have to understand first, but if you already know Drupal 8, then you will have no problem with the aforementioned solution.

Another possibility would be to hide the link from the theme layer, in a Account Menu's preprocess function.

3
  • Thank you very much for your links, I try to read about it. The concept of Drupal 8 are new to me, I'm used to the Drupal 7 way, so it feels like I'm a total beginner... Its really really out of my understanding, why a registration link isn't an out of the box link... Jun 29, 2016 at 16:18
  • You are welcome! If I had to guess, user registration is not something that can always be done by the user, as Drupal allows administrators to restrict user registration. So maybe that's why there's no out of the box functionality the way you want. You could create new menu and add your registration link to that and then from the block layouts page you can add it between main menu and user account menu. And if you have access to the code base, then you could alter the CSS so that your new block will have the properties of .navbar-right .navbar-nav CSS classes.
    – benelori
    Jul 2, 2016 at 18:03
  • Thank you! You were pretty helpful. Thats probably the best way to do it easy, though its not super clean :) Jul 3, 2016 at 0:39
4

Why create new route? There is one already! Via admin panel, create new menu item at 'account' menu. Link hidden after login (RegisterAccessCheck class will hide it).

fill fields:
name = Register
uri = route:user.register

UPDATE1 You will see link as admin, but others - don't

3

We've contributed a small module which solves this issue: https://www.drupal.org/project/registration_link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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