4

I want to show anonymous users a "Register" link.

So, in mymodule.menu.yml:

mymodule.userregister: title: 'Register an Account' url: 'internal:/user/register' weight: 10 menu_name: account 

This adds a register link to the account menu next to the login/logout link-- great! Except it also shows up for authenticated users, who get an "access denied" error if they click it.

I looked in user.links.menu.yml in code/modules/user to try to find something I could use:

user.page: title: 'My account' weight: -10 route_name: user.page menu_name: account user.logout: weight: 10 menu_name: account class: Drupal\user\Plugin\Menu\LoginLogoutMenuLink 

I also checked user.routing.yml:

user.register: path: '/user/register' defaults: _entity_form: 'user.register' _title: 'Create new account' requirements: _access_user_register: 'TRUE' user.logout: path: '/user/logout' defaults: _controller: '\Drupal\user\Controller\UserController::logout' requirements: _user_is_logged_in: 'TRUE' 

I'm stumped. How can I make my menu link only show up for anonymous users?

5
  • 1
    maybe you need _user_is_logged_in: 'FALSE' Commented Feb 20, 2016 at 16:21
  • @NoSssweat Adding _user_is_logged_in: 'FALSE" as a requirement in mymodule.links.menu.yml and clearing caches doesn't hide the link from my admin account when logged in. Commented Feb 20, 2016 at 16:45
  • Are you saying route_name: user.register instead of the url: ... didn't work? Commented Feb 20, 2016 at 22:17
  • @MPD That's what I thought, so I edited, but upon further testing, it looks like route_name: user.register is ok, but like url still shows for admins. Commented Feb 21, 2016 at 3:03
  • This is a really interesting question. Since, you are using the 'internal:' scheme, there is no difference between using that and the route name (though, you should really use the route name. Both will end going through the path validator, and authenticated users should get an access denied. But, there is an AccessDeniedSubscriber to redirect authenticated users to their edit pages. I think that is the root cause of the problem. Commented Feb 21, 2016 at 22:55

1 Answer 1

4

According to Structure of routes

_role: A specific Drupal role, eg. 'administrator'. You can specify multiple ones via "," for AND and "+" for OR logic. Note that, since the roles available may differ between sites, it's recommended to use permission based access restriction when possible.

so try

requirements: _role: anonymous 
4
  • The link still shows up for users with the admin role, but it is hidden for authenticated users, which is good enough for me. Commented Feb 20, 2016 at 18:19
  • 1
    Perhaps it may only show for super admin (user #1) and not the other admins. Commented Feb 20, 2016 at 20:00
  • No, I was testing from an additional admin account. Commented Feb 21, 2016 at 2:50
  • @PatrickKenny okies Commented Feb 21, 2016 at 3:07

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.