3

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?

  • 1
    maybe you need _user_is_logged_in: 'FALSE' – No Sssweat Feb 20 '16 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. – Patrick Kenny Feb 20 '16 at 16:45
  • Are you saying route_name: user.register instead of the url: ... didn't work? – mpdonadio Feb 20 '16 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. – Patrick Kenny Feb 21 '16 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. – mpdonadio Feb 21 '16 at 22:55
3

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
  • The link still shows up for users with the admin role, but it is hidden for authenticated users, which is good enough for me. – Patrick Kenny Feb 20 '16 at 18:19
  • 1
    Perhaps it may only show for super admin (user #1) and not the other admins. – No Sssweat Feb 20 '16 at 20:00
  • No, I was testing from an additional admin account. – Patrick Kenny Feb 21 '16 at 2:50
  • @PatrickKenny okies – No Sssweat Feb 21 '16 at 3:07

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.