- Issue created by @DhruvR
- 🇦🇺Australia larowlan 🇦🇺🏝.au GMT+10
Searching something with SQL that isn't in the database is going to be fairly tricky and not very performant
- 🇮🇳India DhruvR
Hello @Iarowlan,
I see your point about accessing the display name directly from the database.
However, since the display name is derived from a combination of other user fields like first name and last name, we could utilize a concat SQL search approach.Example:
SELECT * FROM users WHERE CONCAT(first_name, ' ', last_name) LIKE '%John%';
It would be beneficial to implement a method or hook that allows us to customize the search criteria for displaying results in the user autocomplete field.
- 🇦🇺Australia larowlan 🇦🇺🏝.au GMT+10
That already exists
You can implement an alter hook to change the plugin class of the selection handler and write your own query logic
I'll dig up the hook names etc tomorrow (on phone now)
- Status changed to Needs review
11 months ago 9:40pm 28 February 2024 - 🇦🇺Australia larowlan 🇦🇺🏝.au GMT+10
So you have two options, create a new entity reference selection plugin - and then configure the field to use that - here's an example from entity hierarchy that shows the parents in the dropdown - https://git.drupalcode.org/project/entity_hierarchy/-/blob/3.x/src/Plugi...
Or you can implement hook_entity_reference_selection_alter and modify the `default:user` class to your own class and it will apply to all places you have that plugin already in use.
Something like
namespace Drupal\yourmodule; use Drupal\user\Plugin\EntityReferenceSelection\UserSelection; class MyUserSelection extends UserSelection { /** * {@inheritdoc} */ protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { $query = parent::buildEntityQuery($match, $match_operator); // Your stuff here. return $query; } }
and then in
yourmodule.module
function yourmodule_entity_reference_selection_alter(&$info): void { if (isset($info['default:user'])) { $info['default:user']['class'] = \Drupal\yourmodule\MyUserSelection::class; }
- 🇺🇸United States smustgrave
@DhruvR can you confirm that resolved it for you?
- Status changed to Fixed
11 months ago 1:03pm 1 March 2024 Automatically closed - issue fixed for 2 weeks with no activity.