We have created a custom permissions that we want to use to prevent unauthorized access to an admin form. Only someone with "administer admin_form" should have access to the page. There is a menu link that was created to allow access to the page.
How can I create a controller that will check if the user has the permission and hide the menu link if the user does not have the permission? If somehow they got access to the menu link path, we would also like to display a message "you do not have access to this page" and hide the form.
We have tried the following:
mymodule.permissions.yml
administer admin_form:
title: 'Administer admin form'
description: 'Allows the ability to change settings'
restrict access: true
mymodule.links.menu.yml
mymodule.settings:
route_name: mymodule.settings
title: 'My module'
description: 'some message'
weight: 99
mymodule.routing.yml
mymodule.settings:
path: /admin/config/user-interface/admin-form
defaults:
_title: 'My module setting'
_form: \Drupal\mymodule\form\AdminForm
requirements:
_permission: 'administer admin_form'