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

1
  • D8 or D7? You have tagged this question with D7...
    – Jimmy Ko
    Commented Aug 19, 2016 at 15:31

3 Answers 3

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.

5
  • Drupal 8 is difficult.... Anyway +1
    – Jimmy Ko
    Commented Aug 19, 2016 at 16:53
  • +1, it actually work. Now is there a way to actually remove the role once the module has been uninstall?
    – cjgratacos
    Commented Aug 19, 2016 at 17:52
  • found solution - just add in the yml dependencies: module: -<module_name> enforced: module: - <module_name>
    – cjgratacos
    Commented Aug 19, 2016 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
    Commented Feb 7, 2018 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. Commented Feb 9, 2018 at 6:24
2

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().

4
  • 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
    Commented Aug 19, 2016 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
    Commented Aug 19, 2016 at 15:50
  • 1
    yep! In d8 hook_install tends to be for things that are based on vars available after bootstrap
    – lexicant
    Commented Aug 19, 2016 at 15:53
  • 1
    @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. Commented Feb 9, 2018 at 6:23
0

I prefer the programatically way instead of config way:

put into the YOUR_MODULE.install

/**
* Implements hook_install().
*/
function YOUR_MODULE_install() {
  $role = \Drupal\user\Entity\Role::create([
  'id' => 'your_role_name',
  'label' => 'YOUR Role Name',
  ]);
  $role->save();
}

/**
* Implements hook_uninstall().
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
function YOUR_MODULE_uninstall() {
  $role = \Drupal\user\Entity\Role::load('your_role_name')->delete();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.