Allow showing autocomplete results for Drupal fields with empty input string

Created on 18 September 2019, about 5 years ago
Updated 28 January 2023, almost 2 years ago

At now Drupal autocomplete form fields (eg entity reference autocomplete) needs to type at least one symbol for show field autocomplete results.

But in many situations users don't know what to type in form field, and good to show top of available options to user via simple mouse click on field, without any typing. Replacing autocomplete to select is bad idea, when select contain 1000+ options.

Solution for this problem is enabling searching autocomplete results with empty string, but this is hardcoded in Drupal core web/core/modules/system/src/Controller/EntityAutocompleteController.php:

  public function handleAutocomplete(Request $request, $target_type, $selection_handler, $selection_settings_key) {
    $matches = [];
    // Get the typed string from the URL, if it exists.
    if ($input = $request->query->get('q')) {

Quick easy hack is enable calling "getMatches" for empty q get argument via changing to this:

  public function handleAutocomplete(Request $request, $target_type, $selection_handler, $selection_settings_key) {
    $matches = [];
    // Get the typed string from the URL, if it exists.
    $input = $request->query->get('q');
    if ($input !== NULL) {

Other solutions maybe use some keyword (eg q=_ALL_) for show all results, use additional argument (?q=&show-empty=1).

On field widget settings side will be good to add separate checkbox "Allow autocomplete for empty string", or maybe combine with "Minimum string length for autocomplete = 0", and separate setting for form fields with entity_autocomplete type.

What do you think about this idea?

Feature request
Status

Needs work

Version

10.1

Component
Entity 

Last updated about 11 hours ago

Created by

🇦🇲Armenia murz Yerevan, Armenia

Live updates comments and jobs are added and updated live.
  • Needs product manager review

    It is used to alert the product manager core committer(s) that an issue represents a significant new feature, UI change, or change to the "user experience" of Drupal, and their signoff is needed. If an issue significantly affects the usability of Drupal, use Needs usability review instead (see the governance policy draft for more information).

  • Needs usability review

    Used to alert the usability topic maintainer(s) that an issue significantly affects (or has the potential to affect) the usability of Drupal, and their signoff is needed. When adding this tag, make it easy to review the issue. Make sure the issue summary describes the problem and the proposed solution. Screenshots usually help a lot! To get sign-off on issues with the "Needs usability review" tag, post about them in the #ux channel on Drupal Slack, and/or attend a UX meeting to demo the patch and get direct feedback from designers/UX folks/product management on next steps. If an issue represents a significant new feature, UI change, or change to the general "user experience" of Drupal, use Needs product manager review instead. See the scope of responsibilities for product managers.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • First commit to issue fork.
  • @gxleano opened merge request.
  • 🇪🇸Spain gxleano Cáceres

    I've created a MR where I'm adding validation for autocomplete minimum sizes in handleAutocomplete into EntityAutocompleteController class.

    Could some FE fellow take over into the frontend part? We'd need to call to system.entity_autocomplete when users click in the autocomplete input. This will trigger the code we have added in the BE for when the match limit is 0.

  • First commit to issue fork.
  • 🇧🇷Brazil diegors Campinas - Brazil

    Fixed the tests, still need FE part as suggested in #22

Production build 0.71.5 2024