Permissions.php in Drupal 10
File
core/modules/user/src/Plugin/views/field/Permissions.php
View source
<?php
namespace Drupal\user\Plugin\views\field;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\field\PrerenderList;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Permissions extends PrerenderList {
protected $roleStorage;
protected $moduleHandler;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->roleStorage = $entity_type_manager
->getStorage('user_role');
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('module_handler'), $container
->get('entity_type.manager'));
}
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->additional_fields['uid'] = [
'table' => 'users_field_data',
'field' => 'uid',
];
}
public function query() {
$this
->addAdditionalFields();
$this->field_alias = $this->aliases['uid'];
}
public function preRender(&$values) {
$this->items = [];
$permission_names = \Drupal::service('user.permissions')
->getPermissions();
$rids = [];
foreach ($values as $result) {
$user_rids = $this
->getEntity($result)
->getRoles();
$uid = $this
->getValue($result);
foreach ($user_rids as $rid) {
$rids[$rid][] = $uid;
}
}
if ($rids) {
$roles = $this->roleStorage
->loadMultiple(array_keys($rids));
foreach ($rids as $rid => $role_uids) {
foreach ($roles[$rid]
->getPermissions() as $permission) {
foreach ($role_uids as $uid) {
$this->items[$uid][$permission]['permission'] = $permission_names[$permission]['title'];
}
}
}
foreach ($this->items as &$permission) {
ksort($permission);
}
}
}
public function render_item($count, $item) {
return $item['permission'];
}
}
Classes
Name |
Description |
Permissions |
Field handler to provide a list of permissions. |