3 namespace Drupal\user\Plugin\Search;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Database\Connection;
7 use Drupal\Core\Entity\EntityManagerInterface;
8 use Drupal\Core\Extension\ModuleHandlerInterface;
9 use Drupal\Core\Session\AccountInterface;
10 use Drupal\Core\Access\AccessibleInterface;
11 use Drupal\search\Plugin\SearchPluginBase;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Executes a keyword search for users against the {users} database table.
19 * title = @Translation("Users")
22 class UserSearch extends SearchPluginBase implements AccessibleInterface {
25 * The database connection.
27 * @var \Drupal\Core\Database\Connection
34 * @var \Drupal\Core\Entity\EntityManagerInterface
36 protected $entityManager;
41 * @var \Drupal\Core\Extension\ModuleHandlerInterface
43 protected $moduleHandler;
48 * @var \Drupal\Core\Session\AccountInterface
50 protected $currentUser;
55 static public function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
57 $container->get('database'),
58 $container->get('entity.manager'),
59 $container->get('module_handler'),
60 $container->get('current_user'),
68 * Creates a UserSearch object.
70 * @param Connection $database
71 * The database connection.
72 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
74 * @param ModuleHandlerInterface $module_handler
76 * @param \Drupal\Core\Session\AccountInterface $current_user
78 * @param array $configuration
79 * A configuration array containing information about the plugin instance.
80 * @param string $plugin_id
81 * The plugin_id for the plugin instance.
82 * @param mixed $plugin_definition
83 * The plugin implementation definition.
85 public function __construct(Connection $database, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, AccountInterface $current_user, array $configuration, $plugin_id, $plugin_definition) {
86 $this->database = $database;
87 $this->entityManager = $entity_manager;
88 $this->moduleHandler = $module_handler;
89 $this->currentUser = $current_user;
90 parent::__construct($configuration, $plugin_id, $plugin_definition);
92 $this->addCacheTags(['user_list']);
98 public function access($operation = 'view', AccountInterface $account = NULL, $return_as_object = FALSE) {
99 $result = AccessResult::allowedIf(!empty($account) && $account->hasPermission('access user profiles'))->cachePerPermissions();
100 return $return_as_object ? $result : $result->isAllowed();
106 public function execute() {
108 if (!$this->isSearchExecutable()) {
112 // Process the keywords.
113 $keys = $this->keywords;
114 // Escape for LIKE matching.
115 $keys = $this->database->escapeLike($keys);
116 // Replace wildcards with MySQL/PostgreSQL wildcards.
117 $keys = preg_replace('!\*+!', '%', $keys);
119 // Run the query to find matching users.
120 $query = $this->database
121 ->select('users_field_data', 'users')
122 ->extend('Drupal\Core\Database\Query\PagerSelectExtender');
123 $query->fields('users', ['uid']);
124 $query->condition('default_langcode', 1);
125 if ($this->currentUser->hasPermission('administer users')) {
126 // Administrators can also search in the otherwise private email field,
127 // and they don't need to be restricted to only active users.
128 $query->fields('users', ['mail']);
129 $query->condition($query->orConditionGroup()
130 ->condition('name', '%' . $keys . '%', 'LIKE')
131 ->condition('mail', '%' . $keys . '%', 'LIKE')
135 // Regular users can only search via usernames, and we do not show them
137 $query->condition('name', '%' . $keys . '%', 'LIKE')
138 ->condition('status', 1);
144 $accounts = $this->entityManager->getStorage('user')->loadMultiple($uids);
146 foreach ($accounts as $account) {
148 'title' => $account->getDisplayName(),
149 'link' => $account->url('canonical', ['absolute' => TRUE]),
151 if ($this->currentUser->hasPermission('administer users')) {
152 $result['title'] .= ' (' . $account->getEmail() . ')';
154 $this->addCacheableDependency($account);
155 $results[] = $result;
164 public function getHelp() {
166 '#theme' => 'item_list',
168 $this->t('User search looks for user names and partial user names. Example: mar would match usernames mar, delmar, and maryjane.'),
169 $this->t('You can use * as a wildcard within your keyword. Example: m*r would match user names mar, delmar, and elementary.'),