Overview

I had the opportunity to use custom permissions in a Drupal custom module, so here are my notes.

Background

In the following article, I introduced a module that executes GitHub Actions from Drupal.

/en/posts/11f85c2ef4fca7/

However, the permission was set to administer site configuration, so only users with administrator privileges could execute it.

The commit addressing this issue is as follows.

https://github.com/nakamura196/Drupal-module-github_webhook/commit/c3b6f57bebfeda0556c929c8ed8ed62a0eb0a5c4

Method

Below, I share the response from ChatGPT 4o.


In Drupal's _permission, you can set values to configure specific access permissions. This controls whether users with specific roles can access that route.

The main values that can be set for _permission are as follows:

Common Permissions

Drupal provides several standard permissions. Here are examples of commonly used permissions:

  1. administer site configuration - Permission to manage site-wide configuration
  2. access content - Permission to access content
  3. administer users - Permission to manage users
  4. create content - Permission to create content
  5. edit any content - Permission to edit any content
  6. delete any content - Permission to delete any content
  7. view user profiles - Permission to view user profiles
  8. administer permissions - Permission to manage permissions
  9. access administration pages - Permission to access administration pages
  10. administer taxonomy - Permission to manage taxonomy

Custom Permissions

You can also create module-specific permissions. For example, you can create custom permissions for the github_webhook module and use them in _permission.

To create custom permissions, define them in github_webhook.permissions.yml as follows:

access github webhook settings:
  title: 'Access GitHub Webhook Settings'
  description: 'Allows users to access and modify GitHub Webhook settings'
  restrict access: TRUE

Then, set this custom permission in routing.yml:

github_webhook.settings:
  path: '/admin/config/github_webhook'
  defaults:
    _form: '\Drupal\github_webhook\Form\SettingsForm'
    _title: 'GitHub Webhook Settings'
  requirements:
    _permission: 'access github webhook settings'

Summary

For _permission, you can set standard Drupal permissions (such as administer site configuration). You can also define custom permissions and create access permissions specific to that module or feature.

Configuration

As a result, the following permissions were added, allowing access to be granted for specific roles.

Summary

I hope this serves as a useful reference for building and using Drupal custom modules.