Mercurial > hg > isophonics-drupal-site
comparison core/modules/user/src/Plugin/Search/UserSearch.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 7a779792577d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\user\Plugin\Search; | |
4 | |
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; | |
13 | |
14 /** | |
15 * Executes a keyword search for users against the {users} database table. | |
16 * | |
17 * @SearchPlugin( | |
18 * id = "user_search", | |
19 * title = @Translation("Users") | |
20 * ) | |
21 */ | |
22 class UserSearch extends SearchPluginBase implements AccessibleInterface { | |
23 | |
24 /** | |
25 * The database connection. | |
26 * | |
27 * @var \Drupal\Core\Database\Connection | |
28 */ | |
29 protected $database; | |
30 | |
31 /** | |
32 * The entity manager. | |
33 * | |
34 * @var \Drupal\Core\Entity\EntityManagerInterface | |
35 */ | |
36 protected $entityManager; | |
37 | |
38 /** | |
39 * The module handler. | |
40 * | |
41 * @var \Drupal\Core\Extension\ModuleHandlerInterface | |
42 */ | |
43 protected $moduleHandler; | |
44 | |
45 /** | |
46 * The current user. | |
47 * | |
48 * @var \Drupal\Core\Session\AccountInterface | |
49 */ | |
50 protected $currentUser; | |
51 | |
52 /** | |
53 * {@inheritdoc} | |
54 */ | |
55 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | |
56 return new static( | |
57 $container->get('database'), | |
58 $container->get('entity.manager'), | |
59 $container->get('module_handler'), | |
60 $container->get('current_user'), | |
61 $configuration, | |
62 $plugin_id, | |
63 $plugin_definition | |
64 ); | |
65 } | |
66 | |
67 /** | |
68 * Creates a UserSearch object. | |
69 * | |
70 * @param Connection $database | |
71 * The database connection. | |
72 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager | |
73 * The entity manager. | |
74 * @param ModuleHandlerInterface $module_handler | |
75 * The module handler. | |
76 * @param \Drupal\Core\Session\AccountInterface $current_user | |
77 * The 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. | |
84 */ | |
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); | |
91 | |
92 $this->addCacheTags(['user_list']); | |
93 } | |
94 | |
95 /** | |
96 * {@inheritdoc} | |
97 */ | |
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(); | |
101 } | |
102 | |
103 /** | |
104 * {@inheritdoc} | |
105 */ | |
106 public function execute() { | |
107 $results = []; | |
108 if (!$this->isSearchExecutable()) { | |
109 return $results; | |
110 } | |
111 | |
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); | |
118 | |
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') | |
132 ); | |
133 } | |
134 else { | |
135 // Regular users can only search via usernames, and we do not show them | |
136 // blocked accounts. | |
137 $query->condition('name', '%' . $keys . '%', 'LIKE') | |
138 ->condition('status', 1); | |
139 } | |
140 $uids = $query | |
141 ->limit(15) | |
142 ->execute() | |
143 ->fetchCol(); | |
144 $accounts = $this->entityManager->getStorage('user')->loadMultiple($uids); | |
145 | |
146 foreach ($accounts as $account) { | |
147 $result = [ | |
148 'title' => $account->getDisplayName(), | |
149 'link' => $account->url('canonical', ['absolute' => TRUE]), | |
150 ]; | |
151 if ($this->currentUser->hasPermission('administer users')) { | |
152 $result['title'] .= ' (' . $account->getEmail() . ')'; | |
153 } | |
154 $this->addCacheableDependency($account); | |
155 $results[] = $result; | |
156 } | |
157 | |
158 return $results; | |
159 } | |
160 | |
161 /** | |
162 * {@inheritdoc} | |
163 */ | |
164 public function getHelp() { | |
165 $help = [ | |
166 'list' => [ | |
167 '#theme' => 'item_list', | |
168 '#items' => [ | |
169 $this->t('User search looks for user names and partial user names. Example: mar would match usernames mar, delmar, and maryjane.'), | |
170 $this->t('You can use * as a wildcard within your keyword. Example: m*r would match user names mar, delmar, and elementary.'), | |
171 ], | |
172 ], | |
173 ]; | |
174 | |
175 return $help; | |
176 } | |
177 | |
178 } |