I am making a CRM management application based on Drupal. It will be used by a number of different organisations so I want it to be configurable. I have made a custom entity type called Person, along with some default Person bundles, e.g. Volunteer and Staff. Organisations can also add their own Person bundles. For example, an organisation giving legal advice might want to add a Lawyer bundle.
I am using the
permissions_callback system to dynamically create permissions. This works, insofar as when a new bundle is created, the permissions relating to that bundle are created too.
The type of user who will create these bundles will not be an overall Administrator but will have a slightly cut-down role of Site Administrator.
When a Site Administrator creates a new bundle I want them to automatically be granted permissions to view, add, edit and delete entities of the new type (e.g. if they create a Lawyer bundle I want them to immediately be able to add new lawyers).
I tried using a
hook_entity_insert to grant the permissions to the role as soon as the config entity (bundle) was created. But this does not work. Debugging has shown me the reason it does not work is that the permissions do not yet exist at the point when the bundle is inserted.
I have thought of one way to solve this which would be to do it all inside the hook: create the permissions, and then grant them to the role. But I can't find any way to create permissions programmatically except by using