I have created a custom module with custom database table in drupal 8 site. It has add, update, list and search pages.

I want to restrict as only admin or sub-admin can access the add, edit, list pages. Any user(non-registered also) can access the search page.

How to achieve this in the module i have developed itself in drupal 8? Or is there any common way in drupal 8 to add permission for a particular URL?

  • What type are those pages? Entities or something totally custom? – Ismini Sep 28 '17 at 15:18
  • It is totally custom page. all pages are forms. created by referred this valuebound.com/resources/blog/… link. I created all the page using the forms – siddiq Sep 28 '17 at 15:26
  • Then I think that you need something like this drupal.org/docs/8/api/routing-system/access-checking-on-routes – Ismini Sep 28 '17 at 15:44
  • Why not just add permission checks in the route definitions? – Kevin Sep 28 '17 at 16:11
  • thanks @Ismini . I used _role to specify roles. and _access to set publicly accessible – siddiq Sep 28 '17 at 17:09

I did that through routing.yml file. Thanks for the comments. I posted this just for reference that may help someone else.

  path: '/notices/addupdate'
    _form: '\Drupal\notices\Form\NoticesAddUpdateForm'
    _title: 'Notices Form'
    _role: 'administrator'

  path: '/notices/search'
    _form: '\Drupal\notices\Form\NoticesSearchForm'
    _title: 'Search Notices'
    _access: 'TRUE'

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.