The following snippet of code is found in the 'nodeaccess' module. At the moment, the autocomplete is allowing you to search for any user in the db. What I would like to do is only return users that have role A or role B.
if ($user->hasPermission('access user profiles')) {
$form['keys'] = [
'#type' => 'entity_autocomplete',
'#default_value' => isset($form_values['keys']) ? $form_values['keys'] : '',
'#size' => 40,
'#target_type' => 'user',
'#title' => $this->t('Enter names to search for users'),
];
}
I found this link that appears to be doing something similar to what i'd like, but it doesn't appear to be working by just swapping out the conditional. I went and modified it to look like the following:
function my_custom_module_query_alter($query) {
if ($query->hasTag('user_acccess')) {
$or = db_or()->condition('user__roles.roles_target_id', 'A')-
>condition('user__roles.roles_target_id', 'B');
$query->condition($or);
}
}
Is it the query that I did incorrectly? Or perhaps it is the tag 'user_access' that isn't matching the first snippet of code above? Any guidance would be greatly appreciated.
Oh and my site is running on Drupal Core 8.9.8 btw