How to determine the name of a Drupal dynamic permission

Here’s something…. funny aka painful.

I tried to take a shortcut in determining a dynamic permission for a workflow by just looking at the names of the classes on the permssions page and I saw this:

edit-permissions-use-study-plan-transition-archive
edit-permissions-use-study-plan-transition-create-new-draft

etc.

One incorrectly thought that maybe the name of the permission was use study plan transition create new draft but no - there is no way to tell which one of those dashes translates to underscores.

Hold the boat

If one looks a bit further (I just did this) in at the markup for the actual checkboxes on the permissions one will then find:

<input class="rid-anonymous js-rid-anonymous form-checkbox" data-drupal-selector="edit-anonymous-use-study-plan-transition-archive" type="checkbox" id="edit-anonymous-use-study-plan-transition-archive" name="anonymous[use study_plan transition create_new_draft]" value="1">

…where use study_plan transition create_new_draftcan be seen.

Before I actually used my eyes and clicked around I was going to say that the code to find these things out is a trivial:

$role = Role::load('admin');
$all_permissions = $role->getPermissions();

etc.

Note that you can also use the EntityTypeManager to load up your roles but the storage is of type user_storage of course!