3 namespace Drupal\user\Plugin\views\filter;
5 use Drupal\user\RoleInterface;
6 use Drupal\user\RoleStorageInterface;
7 use Drupal\views\Plugin\views\filter\ManyToOne;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
11 * Filter handler for user roles.
13 * @ingroup views_filter_handlers
15 * @ViewsFilter("user_roles")
17 class Roles extends ManyToOne {
22 * @var \Drupal\user\RoleStorageInterface
24 protected $roleStorage;
27 * Constructs a Roles object.
29 * @param array $configuration
30 * A configuration array containing information about the plugin instance.
31 * @param string $plugin_id
32 * The plugin_id for the plugin instance.
33 * @param mixed $plugin_definition
34 * The plugin implementation definition.
35 * @param \Drupal\user\RoleStorageInterface $role_storage
38 public function __construct(array $configuration, $plugin_id, $plugin_definition, RoleStorageInterface $role_storage) {
39 parent::__construct($configuration, $plugin_id, $plugin_definition);
40 $this->roleStorage = $role_storage;
46 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
51 $container->get('entity.manager')->getStorage('user_role')
55 public function getValueOptions() {
56 $this->valueOptions = user_role_names(TRUE);
57 unset($this->valueOptions[RoleInterface::AUTHENTICATED_ID]);
58 return $this->valueOptions;
63 * Override empty and not empty operator labels to be clearer for user roles.
65 public function operators() {
66 $operators = parent::operators();
67 $operators['empty']['title'] = $this->t("Only has the 'authenticated user' role");
68 $operators['not empty']['title'] = $this->t("Has roles in addition to 'authenticated user'");
75 public function calculateDependencies() {
77 if (in_array($this->operator, ['empty', 'not empty'])) {
80 foreach ($this->value as $role_id) {
81 $role = $this->roleStorage->load($role_id);
82 $dependencies[$role->getConfigDependencyKey()][] = $role->getConfigDependencyName();