Parts of this article were generated by AI.
Overview
When creating configuration pages for custom modules in Drupal, you may want to allow non-admin users to access them. This article explains how to solve this issue, using the GitHub Webhook module as an example.
The Problem
Initially, the routing configuration was as follows:
With this configuration, only administrators with the administer site configuration permission could access the page, and general users could not.
Solution 1: Creating a Dedicated Permission
First, create a dedicated permission. Create a new github_webhook.permissions.yml file:
Then update the routing file:
The Discovery: /admin Path Restrictions
However, this change alone did not solve the problem. In Drupal, paths under /admin are treated as the admin area and are subject to additional permission checks.
Solution 2: Changing the Path
As a fundamental solution, move the path outside the admin area:
New Problem: Disappearance from the Admin Panel
After moving the path, the item disappeared from the configuration list in the admin panel.
Final Solution: Manually Adding Menu Links
To solve this issue, create a github_webhook.links.menu.yml file:
This allows the menu item to appear under “Services” in the admin panel while keeping the path at /github-webhook/settings.
Customizing the Placement
You can adjust the display location within the admin panel by changing the parent value:
system.admin_config_services- Under “Services”system.admin_config_system- Under “System”system.admin_config_development- Under “Development”system.admin_config- Top level under “Configuration”
Alternative: hook_menu_links_discovered_alter()
Instead of using a .links.menu.yml file, you can also use a hook in the module file:
Granting Permissions
Finally, grant the newly created access github webhook settings permission to the appropriate roles on the “User Permissions” page in the admin panel.
Summary
To allow non-admin users to access configuration pages in Drupal:
- Create a dedicated permission - Define a new permission in the
.permissions.ymlfile - Change the path - Move from under
/adminto a regular path - Add menu links - Display the item in the admin panel using
.links.menu.yml - Grant permissions - Assign the permission to appropriate roles in the admin panel
- Clear cache - Apply the changes with
drush cr
These steps enable flexible permission management, allowing you to implement access control tailored to your project’s needs.
Don’t forget to clear the cache with drush cr whenever you make changes!
Final Notes
There may be some inaccuracies, but I hope this is helpful as a reference.