1

Permissions may be added to existing roles programatically as described in this answer. However, I would like to put that into configuration.

I can create a role calling king by putting user.role.king.yml with the following contents at my_module/config/install/:

langcode: en
status: true
dependencies: {  }
id: king
label: 'An actual king.'
weight: 0
is_admin: false
permissions:
  - 'make war'
  - 'judge'
  - 'collect taxes'
  - 'eat'

But in this kingdom, anonymous users may also eat. Adding user.role.anonymous.yml with the following contents to my_module/config/install/ produces an error:

langcode: en
status: true
dependencies: {  }
id: anonymous
label: 'Anonymous'
weight: 0
is_admin: false
permissions:
  - 'eat'

The error, when trying to re-install using devel:

Unable to reinstall modules. Error: Configuration objects (user.role.anonymous) provided by my_module already exist in active configuration.

So it seems that this config may not be duplicated. Is there a way to put anonymous permissions into config, other than hacking /core/modules/user/config/install/user.role.anonymous.yml?

  • I think: No, config files are to define new config. If you want to alter existing config you need to add a hook_install() and/or hook_update() and update the existing role there. (You can do so with the code you linked to in your question.) Once you do that with the install hook, though, the update will be exported to code with the role the next time you export your config. – sonfd Sep 28 '18 at 0:28
  • Hmm, I was expecting this to be the case. Hopefully this question helps others to fail faster if they are trying to do what I tried to do. – rudolfbyker Sep 28 '18 at 6:49

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.