Skip to content
Snippets Groups Projects

Display the roles label instead of its machine name

Open suraj nirmal requested to merge issue/apply_for_role-3481995:3481995-role-label into 2.0.x
1 unresolved thread
1 file
+ 20
2
Compare changes
  • Side-by-side
  • Inline
@@ -12,7 +12,7 @@ namespace Drupal\apply_for_role\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\apply_for_role\application_manager;
use Drupal\user\Entity\Role;
class ApplyForRoleApplicationForm extends FormBase{
private $application_manager;
@@ -45,7 +45,7 @@ class ApplyForRoleApplicationForm extends FormBase{
}
}
$available_roles = $this->getCleanRoleLabel($available_roles);
// Go through all roles, find roles with a value of 0 and unset them
foreach($available_roles as $available_role_key => $available_role){
if($available_role === 0){
@@ -120,6 +120,24 @@ class ApplyForRoleApplicationForm extends FormBase{
$this->messenger()->addStatus(t('Thank you for submitting an applicaton. Your requested is currently queued for review.'));
}
/**
* This will display the role's label instead of its machine name.
*
* @param array $available_roles
* List available roles.
* @return array
* An array with machine names as keys and corresponding labels as values.
*/
protected function getCleanRoleLabel (array $available_roles): array {
$roles = [];
foreach (Role::loadMultiple() as $role) {
    • Try to do it the other way around, no need to load all roles. You already load just the configured role's machines names from config, loop over those and convert them there.

Please register or sign in to reply
if (isset($available_roles[$role->id()]) && $available_roles[$role->id()]) {
$roles[$role->id()] = $role->label();
}
}
return $roles;
}
/**
* Helper function that fetches the roles a user has.
*
Loading