Permissions.php in Zircon Profile 8
File
core/modules/user/src/Plugin/views/field/Permissions.php
View source
<?php
namespace Drupal\user\Plugin\views\field;
use Drupal\Core\Entity\EntityManagerInterface;
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, EntityManagerInterface $entity_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->roleStorage = $entity_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.manager'));
}
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->additional_fields['uid'] = array(
'table' => 'users_field_data',
'field' => 'uid',
);
}
public function query() {
$this
->addAdditionalFields();
$this->field_alias = $this->aliases['uid'];
}
public function preRender(&$values) {
$uids = array();
$this->items = array();
$permission_names = \Drupal::service('user.permissions')
->getPermissions();
$rids = array();
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 ($uids as $uid) {
if (isset($this->items[$uid])) {
ksort($this->items[$uid]);
}
}
}
}
function render_item($count, $item) {
return $item['permission'];
}
}
Classes
Name |
Description |
Permissions |
Field handler to provide a list of permissions. |