Roles.php in Drupal 10
File
core/modules/user/src/Plugin/views/filter/Roles.php
View source
<?php
namespace Drupal\user\Plugin\views\filter;
use Drupal\user\RoleInterface;
use Drupal\user\RoleStorageInterface;
use Drupal\views\Plugin\views\filter\ManyToOne;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Roles extends ManyToOne {
protected $roleStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, RoleStorageInterface $role_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->roleStorage = $role_storage;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager')
->getStorage('user_role'));
}
public function getValueOptions() {
$this->valueOptions = user_role_names(TRUE);
unset($this->valueOptions[RoleInterface::AUTHENTICATED_ID]);
return $this->valueOptions;
}
public function operators() {
$operators = parent::operators();
$operators['empty']['title'] = $this
->t("Only has the 'authenticated user' role");
$operators['not empty']['title'] = $this
->t("Has roles in addition to 'authenticated user'");
return $operators;
}
public function calculateDependencies() {
$dependencies = [];
if (in_array($this->operator, [
'empty',
'not empty',
])) {
return $dependencies;
}
if (is_string($this->value) && $this->value === '') {
return [];
}
foreach ((array) $this->value as $role_id) {
if ($role = $this->roleStorage
->load($role_id)) {
$dependencies[$role
->getConfigDependencyKey()][] = $role
->getConfigDependencyName();
}
else {
trigger_error("The {$role_id} role does not exist. You should review and fix the configuration of the {$this->view->id()} view.", E_USER_WARNING);
}
}
return $dependencies;
}
}
Classes
Name |
Description |
Roles |
Filter handler for user roles. |