Untitled

mail@pastecode.io avatarunknown
plain_text
2 months ago
1.2 kB
1
Indexable
Never
use Drupal\workflows\Entity\Workflow;
use Drupal\workflows\TransitionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;

// Load all workflow configurations.
$workflow_configs = Workflow::loadMultiple();

// Load the entity type manager.
$entity_type_manager = \Drupal::entityTypeManager();

$roles_by_state = [];

// Loop through each workflow configuration to gather roles by state.
foreach ($workflow_configs as $workflow_config) {
  $transitions = $workflow_config->getTransitions();
  foreach ($transitions as $transition) {
    // Ensure the transition implements TransitionInterface.
    if ($transition instanceof TransitionInterface) {
      // Get the to state of the transition.
      $to_state_id = $transition->to();
      $to_state = $workflow_config->getState($to_state_id);

      // Get roles associated with the state.
      $roles = $to_state->get('roles');
      foreach ($roles as $role_id => $role_name) {
        // Load the role to get its label.
        $role = $entity_type_manager->getStorage('user_role')->load($role_id);
        if ($role) {
          $roles_by_state[$to_state_id][$role_id] = $role->label();
        }
      }
    }
  }
}

// Now $roles_by_state contains roles allowed to act on each state.