Enable search ability of altered display names in user autocomplete field.

Created on 27 February 2024, 8 months ago
Updated 15 March 2024, 7 months ago

Problem/Motivation

Thanks to the hook_user_format_name_alter() hook altering the username that is displayed for a user is really easy.
But the issue is Display name is not searchable in the Authored by field of entries form.

For example: If username is admin but display name is Super Admin.
It's not searchable in the Author field of entity edit (My case node edit form).

Steps to reproduce

  • After implementation of hook when I go to edit or create a node.
  • Search for Display name in the Author field is not working.
  • Only work if the actual user name is entered.
  • In my case, admin is a username and Dhruv is display name

Code Example:

use Drupal\Core\Session\AccountInterface;
use Drupal\user\Entity\User;

/**
 * Implements hook_user_format_name_alter().
 */
function MY_MODULE_user_format_name_alter(&$name, AccountInterface $account) {
  $full_name = [];

  $account = User::load($account->id());
  $first_name = $account->get('field_first_name')->value;
  $last_name = $account->get('field_last_name')->value;

  if (!empty($first_name)) {
    $full_name[] = $first_name;
  }

  if (!empty($last_name)) {
    $full_name[] = $last_name;
  }

  if (!empty($full_name)) {
    $name = implode(' ', $full_name);
  }
}
💬 Support request
Status

Fixed

Version

11.0 🔥

Component
User module 

Last updated 3 days ago

Created by

🇮🇳India DhruvR

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • 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)

  • 🇮🇳India DhruvR

    Thank you!! Will be waiting for your response :) @Iarowlan

  • Status changed to Needs review 8 months ago
  • 🇦🇺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;
    }
    
  • 🇮🇳India DhruvR

    Thank you!! Going to review

  • 🇺🇸United States smustgrave

    @DhruvR can you confirm that resolved it for you?

  • 🇮🇳India DhruvR

    Hello @all, Thank you for help the issue is resolved for me.

  • Status changed to Fixed 8 months ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024