class UserSearch in Drupal 9
Same name and namespace in other branches
- 8 core/modules/user/src/Plugin/Search/UserSearch.php \Drupal\user\Plugin\Search\UserSearch
Executes a keyword search for users against the {users} database table.
Plugin annotation
@SearchPlugin(
id = "user_search",
title = @Translation("Users")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\search\Plugin\SearchPluginBase implements RefinableCacheableDependencyInterface, ContainerFactoryPluginInterface, SearchInterface uses RefinableCacheableDependencyTrait
- class \Drupal\user\Plugin\Search\UserSearch implements AccessibleInterface
- class \Drupal\search\Plugin\SearchPluginBase implements RefinableCacheableDependencyInterface, ContainerFactoryPluginInterface, SearchInterface uses RefinableCacheableDependencyTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of UserSearch
File
- core/
modules/ user/ src/ Plugin/ Search/ UserSearch.php, line 23
Namespace
Drupal\user\Plugin\SearchView source
class UserSearch extends SearchPluginBase implements AccessibleInterface {
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($container
->get('database'), $container
->get('entity_type.manager'), $container
->get('module_handler'), $container
->get('current_user'), $configuration, $plugin_id, $plugin_definition);
}
/**
* Creates a UserSearch object.
*
* @param \Drupal\Core\Database\Connection $database
* The database connection.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
*/
public function __construct(Connection $database, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, AccountInterface $current_user, array $configuration, $plugin_id, $plugin_definition) {
$this->database = $database;
$this->entityTypeManager = $entity_type_manager;
$this->moduleHandler = $module_handler;
$this->currentUser = $current_user;
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this
->addCacheTags([
'user_list',
]);
}
/**
* {@inheritdoc}
*/
public function access($operation = 'view', AccountInterface $account = NULL, $return_as_object = FALSE) {
$result = AccessResult::allowedIf(!empty($account) && $account
->hasPermission('access user profiles'))
->cachePerPermissions();
return $return_as_object ? $result : $result
->isAllowed();
}
/**
* {@inheritdoc}
*/
public function execute() {
$results = [];
if (!$this
->isSearchExecutable()) {
return $results;
}
// Process the keywords.
$keys = $this->keywords;
// Escape for LIKE matching.
$keys = $this->database
->escapeLike($keys);
// Replace wildcards with MySQL/PostgreSQL wildcards.
$keys = preg_replace('!\\*+!', '%', $keys);
// Run the query to find matching users.
$query = $this->database
->select('users_field_data', 'users')
->extend(PagerSelectExtender::class);
$query
->fields('users', [
'uid',
]);
$query
->condition('default_langcode', 1);
if ($this->currentUser
->hasPermission('administer users')) {
// Administrators can also search in the otherwise private email field,
// and they don't need to be restricted to only active users.
$query
->fields('users', [
'mail',
]);
$query
->condition($query
->orConditionGroup()
->condition('name', '%' . $keys . '%', 'LIKE')
->condition('mail', '%' . $keys . '%', 'LIKE'));
}
else {
// Regular users can only search via usernames, and we do not show them
// blocked accounts.
$query
->condition('name', '%' . $keys . '%', 'LIKE')
->condition('status', 1);
}
$uids = $query
->limit(15)
->execute()
->fetchCol();
$accounts = $this->entityTypeManager
->getStorage('user')
->loadMultiple($uids);
foreach ($accounts as $account) {
$result = [
'title' => $account
->getDisplayName(),
'link' => $account
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString(),
];
if ($this->currentUser
->hasPermission('administer users')) {
$result['title'] .= ' (' . $account
->getEmail() . ')';
}
$this
->addCacheableDependency($account);
$results[] = $result;
}
return $results;
}
/**
* {@inheritdoc}
*/
public function getHelp() {
$help = [
'list' => [
'#theme' => 'item_list',
'#items' => [
$this
->t('User search looks for user names and partial user names. Example: mar would match usernames mar, delmar, and maryjane.'),
$this
->t('You can use * as a wildcard within your keyword. Example: m*r would match user names mar, delmar, and elementary.'),
],
],
];
return $help;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheableDependencyTrait:: |
protected | property | Cache contexts. | |
CacheableDependencyTrait:: |
protected | property | Cache max-age. | |
CacheableDependencyTrait:: |
protected | property | Cache tags. | |
CacheableDependencyTrait:: |
public | function | 4 | |
CacheableDependencyTrait:: |
public | function | 4 | |
CacheableDependencyTrait:: |
public | function | 4 | |
CacheableDependencyTrait:: |
protected | function | Sets cacheability; useful for value object constructors. | |
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
RefinableCacheableDependencyTrait:: |
public | function | 1 | |
RefinableCacheableDependencyTrait:: |
public | function | ||
RefinableCacheableDependencyTrait:: |
public | function | ||
RefinableCacheableDependencyTrait:: |
public | function | ||
SearchPluginBase:: |
protected | property | The keywords to use in a search. | |
SearchPluginBase:: |
protected | property | Array of attributes - usually from the request object. | |
SearchPluginBase:: |
protected | property | Array of parameters from the query string from the request. | |
SearchPluginBase:: |
public | function |
Executes the search and builds render arrays for the result items. Overrides SearchInterface:: |
1 |
SearchPluginBase:: |
public | function |
Builds the URL GET query parameters array for search. Overrides SearchInterface:: |
1 |
SearchPluginBase:: |
public | function |
Returns the currently set attributes (from the request). Overrides SearchInterface:: |
|
SearchPluginBase:: |
public | function |
Returns the currently set keywords of the plugin instance. Overrides SearchInterface:: |
|
SearchPluginBase:: |
public | function |
Returns the current parameters set using setSearch(). Overrides SearchInterface:: |
|
SearchPluginBase:: |
public | function |
Returns the search index type this plugin uses. Overrides SearchInterface:: |
2 |
SearchPluginBase:: |
public | function |
Verifies if the values set via setSearch() are valid and sufficient. Overrides SearchInterface:: |
2 |
SearchPluginBase:: |
public | function |
Alters the search form when being built for a given plugin. Overrides SearchInterface:: |
1 |
SearchPluginBase:: |
public | function |
Sets the keywords, parameters, and attributes to be used by execute(). Overrides SearchInterface:: |
1 |
SearchPluginBase:: |
public | function |
Provides a suggested title for a page of search results. Overrides SearchInterface:: |
|
SearchPluginBase:: |
public | function |
Returns whether or not search results should be displayed in admin theme. Overrides SearchInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UserSearch:: |
protected | property | The current user. | |
UserSearch:: |
protected | property | The database connection. | |
UserSearch:: |
protected | property | The entity type manager. | |
UserSearch:: |
protected | property | The module handler. | |
UserSearch:: |
public | function |
Checks data value access. Overrides AccessibleInterface:: |
|
UserSearch:: |
public static | function |
Creates an instance of the plugin. Overrides SearchPluginBase:: |
|
UserSearch:: |
public | function |
Executes the search. Overrides SearchInterface:: |
|
UserSearch:: |
public | function |
Returns the searching help. Overrides SearchPluginBase:: |
|
UserSearch:: |
public | function |
Creates a UserSearch object. Overrides PluginBase:: |