Small Module, Big Headache Relief for Editorial Settings

This is a small module but it will save you a big headache, especially in editorial settings.

There are two places you'll be looking for entity reference users:

Authored By Field

The first field is "authored by", found under authoring information. It's available in any content type you create. In order to see that field as a content editor, you must have the permission to "administer content".

Custom Entity Reference User Field

The second place where you will need to reference users is to add a custom field in a content type as an entity reference user. This field has a configuration form that asks you to fill out "reference method", "filter by", and "sort by".

In Drupal core, /core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php this plugin provides the entity selection for the user. In order to view all users, blocked/active, you must have the permission "administer users".

<pre class="codeblock"><code class="language-php">if (!$this-&gt;currentUser-&gt;hasPermission('administer users')) {
    $query-&gt;condition('status', 1);
}

Why Did I Create This Module?

I want content editors to have the ability to reference all users (blocked and active) in the Authored by field or in any custom entity reference user field.

There is some work available on Drupal to create a patch that gives you the ability to include the blocked users in the configuration form for the custom field. But, there is no solution for the "authored by" field, because the authored by field does not have a configuration form. Read more about the issue here.

I wanted one solution for both scenarios (the "Authored by" and the custom entity reference user"). The solution is to create a custom permission that can be assigned to any role in the system, that allows that role to reference all users (blocked and active). That's what this module does.

What's New in 2.0

  • Search by email address: for anyone holding the permission, the user autocomplete now matches on the email address as well as the username. Drupal core only ever matches the username, but editors often remember a person by their email.
  • Drupal 12 ready: the selection plugin now uses the modern #[EntityReferenceSelection] PHP attribute instead of the deprecated annotation, and ships with automated test coverage.

Requirements

Drupal 11.3 or 12. No other dependencies. (Need Drupal 9 or 10? Use the 1.x branch.)

Permissions

Grant the "Reference blocked users" permission to any role that should be able to reference active and blocked users in user reference fields, including "Authored by". Users who already have Administer users keep Drupal core's default behavior.

Testing Process

Login as an Admin and Create Four Users and One Role

reference-blocked-users-roles

Install the module and apply the permission to the editor role.

assign-permission-to-editor

Create Two Entity Reference User Fields Inside Article Content Type

We are testing the configuration and the new permission.

First Field: Include Anonymous User and All Roles in the Configuration Form

reference-all-users

Second Field: Exclude Anonymous User and Filter by Editor Role Only in the Configuration Form

reference-all-editors

Logout and Sign In as an Editor and Try to Add or Edit an Article

For Authored By Field, the Field Will Reference All Users

authored-by-reference

For the First New Added Field, the Field Will Reference All Users

view-all-users

For the Second New Added Field, the Field Will Reference All Editors

view-all-editors

Search by Username or Email

In any of these fields, start typing either a username or an email address and the matching user will appear in the autocomplete, active or blocked.


Support and maintenance

Questions, ideas, and bug reports are welcome. Please use this project’s issue queue so discussions and solutions remain available to the Drupal community.

Maintainers monitor issues, but response times depend on availability.

We can Drupal it

Developed and maintained by Alaa Haddad of Flash Web Center, LLC.

AI disclosure: AI tools assisted with drafting and reviewing this project description. The maintainer verified the final content against the project source.

Supporting organizations: 
FlashWebCenter.com: Drupal Developments Services.
DrupalCare.com: Drupal Update & Maintenance Services.

Project information

Releases