3 namespace Drupal\user\Plugin\views\filter;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Extension\ModuleHandlerInterface;
7 use Drupal\user\PermissionHandlerInterface;
8 use Drupal\views\Plugin\views\filter\ManyToOne;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
12 * Filter handler for user roles.
14 * @ingroup views_filter_handlers
16 * @ViewsFilter("user_permissions")
18 class Permissions extends ManyToOne {
21 * The permission handler.
23 * @var \Drupal\user\PermissionHandlerInterface
25 protected $permissionHandler;
30 * @var \Drupal\Core\Extension\ModuleHandlerInterface
32 protected $moduleHandler;
35 * Constructs a Permissions object.
37 * @param array $configuration
38 * A configuration array containing information about the plugin instance.
39 * @param string $plugin_id
40 * The plugin_id for the plugin instance.
41 * @param mixed $plugin_definition
42 * The plugin implementation definition.
43 * @param \Drupal\user\PermissionHandlerInterface $permission_handler
44 * The permission handler.
45 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
48 public function __construct(array $configuration, $plugin_id, $plugin_definition, PermissionHandlerInterface $permission_handler, ModuleHandlerInterface $module_handler) {
49 parent::__construct($configuration, $plugin_id, $plugin_definition);
51 $this->permissionHandler = $permission_handler;
52 $this->moduleHandler = $module_handler;
58 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
63 $container->get('user.permissions'),
64 $container->get('module_handler')
68 public function getValueOptions() {
69 if (!isset($this->valueOptions)) {
70 $permissions = $this->permissionHandler->getPermissions();
71 foreach ($permissions as $perm => $perm_item) {
72 $provider = $perm_item['provider'];
73 $display_name = $this->moduleHandler->getName($provider);
74 $this->valueOptions[$display_name][$perm] = Html::escape(strip_tags($perm_item['title']));
76 return $this->valueOptions;
79 return $this->valueOptions;
86 * Replace the configured permission with a filter by all roles that have this
89 public function query() {
90 // @todo user_role_names() should maybe support multiple permissions.
92 // Get all role IDs that have the configured permissions.
93 foreach ($this->value as $permission) {
94 $roles = user_role_names(FALSE, $permission);
95 // user_role_names() returns an array with the role IDs as keys, so take
96 // the array keys and merge them with previously found role IDs.
97 $rids = array_merge($rids, array_keys($roles));
99 // Remove any duplicate role IDs.
100 $rids = array_unique($rids);
101 $this->value = $rids;
103 // $this->value contains the role IDs that have the configured permission.