1

I am trying to create drupal 8 roles and permissions programatically. My current approach is to have a module that when install it will generate those set of roles and permissions defined by the module. It would be more like a background Module, where no form is needed or anything. My problem lies that I really don't know where to start the the roles creations since no form is needed, I mean should I create them inside a hook?, if so which hook?, any other place?

I really don't know if this is the best approach, still a newby on drupal 8. Any suggestions will be greatly appreciate it.

Thanks in advance

  • D8 or D7? You have tagged this question with D7... – Jimmy Ko Aug 19 '16 at 15:31
2

Create the roles on your development site and grab the configuration for them and place them in:

<modulename>/config/install 

When the module is installed it will import that configuration. hook_install is deprecated for installing configuration with a module.

Remove any uuid information from the export as it won't be applicable in most sites.

If you are unaware how to export/import configuration in drupal 8, you should read: D8 Configuration Management

edit: Just as an aside fact, you can in theory write your own config.yml files however until you are very familiar it's easier to just create the role, and export it.

  • Drupal 8 is difficult.... Anyway +1 – Jimmy Ko Aug 19 '16 at 16:53
  • +1, it actually work. Now is there a way to actually remove the role once the module has been uninstall? – cjgratacos Aug 19 '16 at 17:52
  • found solution - just add in the yml dependencies: module: -<module_name> enforced: module: - <module_name> – cjgratacos Aug 19 '16 at 18:13
  • This doesn't work for me, I get the error Configuration objects (user.role.anonymous) provided by my_module already exist in active configuration – Felix Eve Feb 7 '18 at 2:31
  • 1
    @FelixEve This is because this is not fully correct answer. It does not work for you because you already have this configuration active. Module developers should account for that and use install and un-install hooks to alter existing configuration. Drupal 8 does not support partial configs in yaml files. – Alex.Designworks Feb 9 '18 at 6:24
1

If it's during install, I would say you put it in the install hook. When the module gets uninstalled, you can remove the roles again with hook_uninstall().

  • 1
    This is correct for drupal 7, however in drupal 8 you can place them in <modulename>/src/config/install for things stored in active config. – lexicant Aug 19 '16 at 15:46
  • 1
    Fair enough. My solution actually works for Drupal 8 too (the links go to the D8 versions of those hooks), but your option is preferable if it cleans up after uninstalling the module as well. – Raf Aug 19 '16 at 15:50
  • 1
    yep! In d8 hook_install tends to be for things that are based on vars available after bootstrap – lexicant Aug 19 '16 at 15:53
  • @lexicant your solution will not work if you need to, say, assign permissions to existing roles already exported as config. Drupal 8 does not allow to use partial configs. So using install hooks should be correct answer. Unfortunately, thus specific answer does not have an example code. – Alex.Designworks Feb 9 '18 at 6:23

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.