Filter user entity reference field options to active users only
Option 1 (recommended): custom entity reference selection handler. Further options down below.
After implementing that plugin, you need to activate it on the entity form display for the field you want it.
Issue:
In a Drupal entity reference field configured to reference user accounts, the select list currently includes all users, including blocked or deactivated accounts.
Expected behavior: only active users should be available for selection by default, or there should be a configurable option to exclude inactive accounts from the reference field results.
Solution:
See the three examples described above.
Option 1 (custom Selection handler, see above)
The best general-purpose solution. It uses the exact extension point Drupal designed for this problem, is scoped precisely to the fields where you assign it, is trivially unit/kernel-testable, and doesn't rely on fragile internals like query tags and metadata that can silently break across core updates.
Option 2: Query tag alter
Avoid this unless you have a very narrow, temporary need and full control over exactly which query is being altered. It's the least robust of the three and the easiest to get wrong (over-broad conditions affecting unrelated queries, or under-broad checks that silently stop working after a core update).
Option 3: entity reference view display
- Edit your Entity Reference field and select "Views: Filter by an entity reference view" as the reference method.
- Create or select a View that displays only the entities you want to show. Configure filters, sorting, and fields as needed.
The View's filters automatically control which options appear in the dropdown. No custom code required – it's all configured through the UI.
This can be chosen if (sitebuilders/editors need to have permissions to edit the view and maybe there is a config_ignore for that to prevent overrides during deployments):
- Site builders/editors need to tweak the filtering criteria without a deployment (e.g., "also exclude role X" added later via UI), or
- You need additional Views features (exposed filters, sorting by last login, relationships to other entities) in the same selection.