class PfmUserPermissionsForm in Permissions filtered by modules 8
Provides the user permissions administration PFM module form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\user\Form\UserPermissionsForm
- class \Drupal\pfm\Form\PfmUserPermissionsForm
- class \Drupal\user\Form\UserPermissionsForm
Expanded class hierarchy of PfmUserPermissionsForm
1 string reference to 'PfmUserPermissionsForm'
File
- src/
Form/ PfmUserPermissionsForm.php, line 11
Namespace
Drupal\pfm\FormView source
class PfmUserPermissionsForm extends UserPermissionsForm {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'pfm_user_admin_permissions';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$input = $form_state
->getUserInput();
$form['#prefix'] = '<div id="pfm-user-admin-permissions-wrapper">';
$form['#suffix'] = '</div>';
$form['permissions_filter'] = [
'#type' => 'fieldset',
'#title' => 'Permissions Filters',
'#weight' => -10,
'#attributes' => [
'class' => [
'form--inline',
'clearfix',
],
],
];
$role_names = [];
$role_permissions = [];
$admin_roles = [];
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();
}
$roles_options = [
'all' => 'All Roles',
] + $role_names;
$form['permissions_filter']['roles'] = [
'#type' => 'select',
'#options' => $roles_options,
'#title' => $this
->t('Select Role'),
'#multiple' => TRUE,
'#default_value' => 'all',
'#weight' => -8,
'#ajax' => [
'callback' => [
$this,
'ajaxRefresh',
],
'wrapper' => 'pfm-user-admin-permissions-wrapper',
'progress' => [
'type' => 'fullscreen',
],
],
];
// Store $role_names for use when saving the data.
$form['role_names'] = [
'#type' => 'value',
'#value' => $role_names,
];
// Render role/permission overview:
$form['permissions'] = [
'#type' => 'table',
'#header' => [
$this
->t('Permission'),
],
'#id' => 'permissions',
'#attributes' => [
'class' => [
'permissions',
'js-permissions',
],
],
'#sticky' => TRUE,
'#empty' => $this
->t('Please choose at least one module.'),
];
$selected_roles = isset($input['roles']) ? $input['roles'] : [];
$selected_roles = array_intersect($selected_roles, array_keys($roles_options));
foreach ($role_names as $role_id => $name) {
if (in_array('all', $selected_roles) || in_array($role_id, $selected_roles)) {
$form['permissions']['#header'][] = [
'data' => $name,
'class' => [
'checkbox',
],
];
}
}
$permissions = $this->permissionHandler
->getPermissions();
$providers = $permissions_by_provider = [];
foreach ($permissions as $permission_name => $permission) {
$permissions_by_provider[$permission['provider']][$permission_name] = $permission;
$providers[] = $permission['provider'];
}
$providers = array_unique($providers);
$modules_list = array_keys($this->moduleHandler
->getModuleList());
$modules_list = array_intersect($modules_list, $providers);
$modules_options = array_map(function ($module_name) {
return $this->moduleHandler
->getName($module_name);
}, array_combine($modules_list, $modules_list));
natcasesort($modules_options);
$selected_modules = isset($input['modules']) ? $input['modules'] : [];
$selected_modules = array_intersect($selected_modules, $modules_list);
$form['permissions_filter']['modules'] = [
'#type' => 'select',
'#options' => $modules_options,
'#title' => $this
->t('Select Modules'),
'#multiple' => TRUE,
'#weight' => -9,
'#ajax' => [
'callback' => [
$this,
'ajaxRefresh',
],
'wrapper' => 'pfm-user-admin-permissions-wrapper',
'progress' => [
'type' => 'fullscreen',
],
],
];
if (empty($selected_modules)) {
return $form;
}
// Move the access content permission to the Node module if it is installed.
if ($this->moduleHandler
->moduleExists('node')) {
// Insert 'access content' before the 'view own unpublished content' key
// in order to maintain the UI even though the permission is provided by
// the system module.
$keys = array_keys($permissions_by_provider['node']);
$offset = (int) array_search('view own unpublished content', $keys);
$permissions_by_provider['node'] = array_merge(array_slice($permissions_by_provider['node'], 0, $offset), [
'access content' => $permissions_by_provider['system']['access content'],
], array_slice($permissions_by_provider['node'], $offset));
unset($permissions_by_provider['system']['access content']);
}
$table_build_info = [
$permissions_by_provider,
$role_names,
$role_permissions,
$admin_roles,
$selected_modules,
$selected_roles,
];
// Add method for possible overrides if needed.
$this
->fillPermissionTable($form, $form_state, $table_build_info);
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save permissions'),
'#button_type' => 'primary',
];
$form['#attached']['library'][] = 'user/drupal.user.permissions';
// Add extra functionality if permissions_dragcheck module exist.
if ($this->moduleHandler
->moduleExists('permissions_dragcheck')) {
// Add the library:
$form['#attached']['library'][] = 'permissions_dragcheck/drag-check-js';
// Init:
$form['#attached']['library'][] = 'permissions_dragcheck/permissions-drag-check';
}
return $form;
}
/**
* Helper method for filling Permission Table.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $table_build_info
* The current table build information.
*/
protected function fillPermissionTable(array &$form, FormStateInterface &$form_state, array $table_build_info) {
list($permissions_by_provider, $role_names, $role_permissions, $admin_roles, $selected_modules, $selected_roles, ) = $table_build_info;
foreach ($permissions_by_provider as $provider => $permissions) {
// Module name.
if (!in_array($provider, $selected_modules)) {
continue;
}
$form['permissions'][$provider] = [
[
'#wrapper_attributes' => [
'colspan' => count($role_names) + 1,
'class' => [
'module',
],
'id' => 'module-' . $provider,
],
'#markup' => $this->moduleHandler
->getName($provider),
],
];
foreach ($permissions as $perm => $perm_item) {
// Fill in default values for the permission.
$perm_item += [
'description' => '',
'restrict access' => FALSE,
'warning' => !empty($perm_item['restrict access']) ? $this
->t('Warning: Give to trusted roles only; this permission has security implications.') : '',
];
$form['permissions'][$perm]['description'] = [
'#type' => 'inline_template',
'#template' => '<div class="permission"><span class="title">{{ title }}</span>{% if description or warning %}<div class="description">{% if warning %}<em class="permission-warning">{{ warning }}</em> {% endif %}{{ description }}</div>{% endif %}</div>',
'#context' => [
'title' => $perm_item['title'],
],
];
$form['permissions'][$perm]['description']['#context']['description'] = $perm_item['description'];
$form['permissions'][$perm]['description']['#context']['warning'] = $perm_item['warning'];
foreach ($role_names as $rid => $name) {
if (in_array('all', $selected_roles) || in_array($rid, $selected_roles)) {
$checked = in_array($perm, $role_permissions[$rid]) ? TRUE : FALSE;
$form['permissions'][$perm][$rid] = [
'#title' => $name . ': ' . $perm_item['title'],
'#title_display' => 'invisible',
'#wrapper_attributes' => [
'class' => [
'checkbox',
],
],
'#type' => 'checkbox',
'#default_value' => $checked,
'#attributes' => [
'class' => [
'rid-' . $rid,
'js-rid-' . $rid,
],
'checked' => $checked,
],
'#parents' => [
$rid,
$perm,
],
];
// Show a column of disabled but checked checkboxes.
if ($admin_roles[$rid]) {
$form['permissions'][$perm][$rid]['#disabled'] = TRUE;
$form['permissions'][$perm][$rid]['#default_value'] = TRUE;
}
}
}
}
}
}
/**
* AJAX refresh callback for the form.
*/
public function ajaxRefresh(&$form, FormStateInterface $form_state) {
return $form;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PfmUserPermissionsForm:: |
public | function | AJAX refresh callback for the form. | |
PfmUserPermissionsForm:: |
public | function |
Form constructor. Overrides UserPermissionsForm:: |
|
PfmUserPermissionsForm:: |
protected | function | Helper method for filling Permission Table. | |
PfmUserPermissionsForm:: |
public | function |
Returns a unique string identifying the form. Overrides UserPermissionsForm:: |
|
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. | |
UserPermissionsForm:: |
protected | property | The module handler. | |
UserPermissionsForm:: |
protected | property | The permission handler. | |
UserPermissionsForm:: |
protected | property | The role storage. | |
UserPermissionsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
UserPermissionsForm:: |
protected | function | Gets the roles to display in this form. | 1 |
UserPermissionsForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
UserPermissionsForm:: |
public | function | Constructs a new UserPermissionsForm. |