Problem/Motivation

If you try to customize autocomplete labels, you're thwarted by autocomplete js, which appends the key rather than the value when an item is selected.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

CommentFileSizeAuthor
#2 3088348.patch794 bytescatch

Comments

catch created an issue. See original summary.

catch’s picture

Status: Active » Needs review
StatusFileSize
new794 bytes
plach’s picture

joegl’s picture

Where does the value end up when this patch is applied? Is it still in $form_state (either userInput or the field value)?

This would be a huge improvement for UI/UX!

joegl’s picture

I did NOT apply this patch on 8.8. I DID change this one line of code on my 8.7 install.

All this patch does is set the field value in the form to the autocomplete label, and then you lose the actual value entirely. It creates the new problem of not having the value you need (i.e., the id reference for a node, term, entity, etc.,).

Unless something has changed from 8.7 to 8.8 that renders the above moot, this is a regression imo. Otherwise please ignore me.

EDIT: The workaround I've been using is creating a hidden field in the form to store the autocomplete value, with the same key as the autocomplete field and with "_value" appended. The custom selectHandler checks for that form field and fills the value there instead if it's found, and then will insert the label into the actual autocomplete field. It's by no means a foolproof or comprehensive solution, and I feel it could be improved. But it is much much better UI/UX for our user's than what is in core.

// override drupal.autocomplete object with custom functions
  if(typeof Drupal.autocomplete !== "undefined") {
    // use custom customSelectHandler
    Drupal.autocomplete.options.select = customSelectHandler;
  }


  /**
   * Override drupal selectHandler function
   *
   * @param {jQuery.Event} event
   *   The event triggered.
   * @param {object} ui
   *   The jQuery UI settings object.
   *
   * @return {bool}
   *   Returns false to indicate the event status.
   */
  function customSelectHandler(event, ui) {
    var valueField = $(event.target);
    if($('input[name='+event.target.name+'_value'+']').length > 0) {
      valueField = $('input[name='+event.target.name+'_value'+']');
      // update the labels too
      const labels = Drupal.autocomplete.splitValues(event.target.value);
      labels.pop();
      labels.push(ui.item.label);
      event.target.value = labels.join(', ');
    }
    const terms = Drupal.autocomplete.splitValues(valueField.val());
    // Remove the current input.
    terms.pop();
    // Add the selected item.
    terms.push(ui.item.value);

    valueField.val(terms.join(', '));
    // Return false to tell jQuery UI that we've filled in the value already.
    return false;
  }
imclean’s picture

joegl’s picture

That's a really long issue thread, and as far as I can tell from reviewing the thread and patch, it's specifically about entity reference fields. It doesn't appear to affect the core implementation of the JQuery UI autocomplete in regards to forms and render arrays in custom modules, which is what this patch affects.

We are not using entity references, views, or entity autocompletes in our custom module. We are using our own routes and matching methods. The code in this patch, which effects the global JQuery UI selectHandler function for Drupal Core, would completely break all our forms with autocomplete's and custom routes, as we would not have the value from the autocomplete available to us anywhere in the form, because the label is being inserted where the value currently is.

Unless I am seriously missing something, I don't see how that specific issue is relevant to the global JQuery Autocomplete implementation. (I am entirely open to being wrong though lol)

catch’s picture

Status: Needs review » Needs work

All this patch does is set the field value in the form to the autocomplete label, and then you lose the actual value entirely. It creates the new problem of not having the value you need (i.e., the id reference for a node, term, entity, etc.,).

You're right. I was testing with taxonomy terms, where it actually works fine (i.e. where $entity->label() matches the result looked up in the database, due to Drupal\Core\Entity\Element\EntityReference::matchEntityByTitle()), but this breaks down as soon as $entity->label() returns something which won't match - like a user display name.

amateescu’s picture

Status: Needs work » Closed (duplicate)

Sadly, this problem is "by design" because that's how jQuery UI Autocomplete works: it displays the labels in the autocomplete dropdown and it needs the values in the textfield form element.

The issue referenced by @plach in #3 has an idea for how to overcome this UX annoyance, and #3076171: Provide a new library to replace jQuery UI autocomplete makes it go away completely because Awesomplete inserts the labels into the textfield :)

Closing as a duplicate of #2881892: UX: Hide entity ID in autocomplete widget.

catch’s picture

awesomplete actually seems to make this worse, at least the current implementation at https://www.drupal.org/project/drupal/issues/3076171#comment-13314207

joegl’s picture

Sadly, this problem is "by design" because that's how jQuery UI Autocomplete works: it displays the labels in the autocomplete dropdown and it needs the values in the textfield form element.

I do not fully agree with this, as you can see from my own example code in this very issue. jQuery UI Autocomplete is flexible enough, it's the drupal core implementation of it that appears to demand the value be placed into the textfield. I believe there is an avenue for holding the value for autocomplete fields elsewhere in the $form_state, possibly a dynamically generated hidden field, instead of in the textfield itself, and then pushing the label into the textfield for UI/UX purpose. As long as the way the autocomplete value is stored is standardized within Drupal forms, there should be a way to implement this with jQuery UI Autocomplete.

joegl’s picture

Status: Closed (duplicate) » Needs work

I also want to re-open this, as it's separate from #3076171: Provide a new library to replace jQuery UI autocomplete, which explicitly addresses the autocomplete functionality for Drupal entity autocompletes, whereas this issue more generally applies to the current core implementation of the global JQuery UI Autocomplete object, and will affect every single autocomplete field, not just Drupal entity/term/node reference fields.

I was reading the wrong issue. This probably does belong with #3076171: Provide a new library to replace jQuery UI autocomplete. This issue addresses the most specific problem and motivation for that issue though.

rithesh bk’s picture

Assigned: Unassigned » rithesh bk

currently i am working on it ...

rithesh bk’s picture

Assigned: rithesh bk » Unassigned

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

jonathanshaw’s picture

Version: 8.9.x-dev » 9.1.x-dev
Status: Needs work » Postponed
Related issues: +#3076171: Provide a new library to replace jQuery UI autocomplete

Sounds like this is effectively postponed on #3076171: Provide a new library to replace jQuery UI autocomplete

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.