Score:0

Filtering the result set of entity_autocomplete

ml flag

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

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.