View source
<?php
namespace Drupal\user\Plugin\Search;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\PagerSelectExtender;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessibleInterface;
use Drupal\search\Plugin\SearchPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class UserSearch extends SearchPluginBase implements AccessibleInterface {
protected $database;
protected $entityTypeManager;
protected $moduleHandler;
protected $currentUser;
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);
}
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',
]);
}
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();
}
public function execute() {
$results = [];
if (!$this
->isSearchExecutable()) {
return $results;
}
$keys = $this->keywords;
$keys = $this->database
->escapeLike($keys);
$keys = preg_replace('!\\*+!', '%', $keys);
$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')) {
$query
->fields('users', [
'mail',
]);
$query
->condition($query
->orConditionGroup()
->condition('name', '%' . $keys . '%', 'LIKE')
->condition('mail', '%' . $keys . '%', 'LIKE'));
}
else {
$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;
}
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;
}
}