Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Create a custom permission in Drupal 8

By editor , 5 December, 2015
Question

How to create a custom permission in Drupal 8 ?

Example with a module 'mymodule'

Step 1.
Create if not exist yet, mymodule.permissions.yml (root of mymodule)
Add your new permission like:

mymodule test_permission:
    title: 'My Test Permission'
    description: 'The description'
    restrict access: false

Step 2.
On your mymodule.routing.yml
mymodule.home:
  path: 'test/home'
  defaults:
    _controller: '\Drupal\mymodule\Controller\Test::home'
    _title: 'My test Home'
  requirements:
    _permission: 'mymodule test_permission'

Set permissions on : /admin/people/permissions

 

Permissions AND condition

Must have all
Example:
  requirements:
    _permission: 'mymodule test_permission, mymodule permission'

Permissions ORcondition

Must have at least one
Example:
  requirements:
    _permission: 'mymodule test_permission+mymodule permission'

Tags

  • Drupal 8
  • Code
  • Permission
  • Drupal 9
  • Add new comment

Comments2

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Tim Kariuki (not verified)

9 years 1 month ago

This is why I love Drupal. I…

This is why I love Drupal. I thought it would take forever to achieve this but just as you have demonstrated, with a few lines of code, I have managed to restrict access to the correct user roles. Thanks a lot.

  • Reply

Hagit (not verified)

8 years 10 months ago

Thank you

very helpfull..

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer