Skip to content
Snippets Groups Projects
Commit 4728d82a authored by Andrei Vesterli's avatar Andrei Vesterli
Browse files

Issue #3407115 by dmitri.daranuta: Add a multiple-select role filtering

parent 594563fb
No related branches found
No related tags found
1 merge request!1#3407115: Add the multiple roles select feature.
......@@ -2,8 +2,10 @@
namespace Drupal\better_permissions_page\Form;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\Form\UserPermissionsForm;
use Drupal\user\RoleInterface;
/**
* Provides a better & faster user permissions administration form.
......@@ -43,6 +45,28 @@ class BetterPermissionsForm extends UserPermissionsForm {
'#description' => $this->t('Select a permission provider to fetch the table with permissions.'),
];
$form['roles'] = [
'#type' => 'select',
'#multiple' => TRUE,
'#title' => $this->t('Roles:'),
'#options' => array_map(fn(RoleInterface $role) => Html::escape($role->label()), $this->roleStorage->loadMultiple()),
'#ajax' => [
'callback' => '::getPermissions',
'event' => 'change',
'progress' => [
'type' => 'throbber',
'message' => $this->t('Fetching permissions...'),
],
'wrapper' => 'permissions-wrapper',
],
'#states' => [
'invisible' => [
'select[name="provider"]' => ['value' => '_none'],
],
],
'#description' => $this->t('Choose which roles to display.')
];
$form['permissions'] = [
'#type' => 'table',
'#title' => $this->t('Permissions'),
......@@ -58,13 +82,16 @@ class BetterPermissionsForm extends UserPermissionsForm {
/** @var \Drupal\user\RoleInterface $role */
foreach ($this->getRoles() as $role_name => $role) {
// Retrieve role names for columns.
$role_names[$role_name] = $role->label();
// Fetch permissions for the roles.
$role_permissions[$role_name] = $role->getPermissions();
$admin_roles[$role_name] = $role->isAdmin();
$form['permissions']['#header'][] = $role->label();
$roles = $form_state->getValue('roles');
if (empty($roles) || in_array($role_name, $roles)) {
// Retrieve role names for columns.
$role_names[$role_name] = $role->label();
// Fetch permissions for the roles.
$role_permissions[$role_name] = $role->getPermissions();
$admin_roles[$role_name] = $role->isAdmin();
// Populate the permissions table header with the remaining columns.
$form['permissions']['#header'][] = $role->label();
}
}
// Store $role_names for use when saving the data.
......@@ -145,14 +172,14 @@ class BetterPermissionsForm extends UserPermissionsForm {
$permissions = $form_state->getValue('permissions');
if ($permissions) {
$mappped = [];
$mapped = [];
foreach ($permissions as $perm_name => $roles) {
foreach ($roles as $role => $checked) {
$mappped[$role][$perm_name] = $checked;
$mapped[$role][$perm_name] = $checked;
}
}
foreach ($mappped as $role_name => $perms) {
foreach ($mapped as $role_name => $perms) {
// Update the permission for the context role/permission.
user_role_change_permissions($role_name, $perms);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment