I created this issue and snippet as better entityreference.module autocomplete widget usability solution in: 2571107.do
Today I'd like to discuss a problem that lead to usability problems in several Drupal 7 projects already (and this is why I'm categorizing this as 'Bug report' and not as 'Feature request').
The autocomplete widget is nice, but additionally to other usability problems which already have their own issues, the ENTER keydown behaviour is not very nice and hard to understand for normal users.
How to reproduce the usability problem:
Enter a value (in my case from an entity reference views results filter) into the ER autocomplete field, e.g. "401"
When the select list appears do NOT click on a value and do NOT click arrow down but press ENTER directly. A user would do this if he entered a value that is exactly the same as in the list, e.g. "401"
Now the value "401" seems to be selected correctly but it is not, because the ID part is missing ("401 (123)")
Form validation fails and other fields (in my case from entityreference_autofill) don't work correctly.
A clever workaround?
One of the long-term reasons for this problems is, that the ID in the brackets is required, so that entering the same value as the correct result is not enough.
But hey, the first value is in most cases exactly what the user was searching for. So why don't we simply select the first result value on enter click, if there is at least one matching entry (or perhaps EXACTLY one?)
In our usability tests this improved the understanding a lot. And if the user presses enter before there are selectable results we trigger an alert.
The key point is: This all is possible with the current implementation of entityreference autocomplete widget without any further technical problems or drawbacks.
Perhaps one day the entityreference autocomplete might become better at all but until this we should have a look how we can improve usability.
Here's my JS implementation that can be added by a custom module in hook_init or could be directly patched into entityreference if we can get enough positive feedback!
/**
* Implements hook_init.
*/
function MY_MODULE_init() {
drupal_add_js("(function ($) {
'use strict';
Drupal.behaviors.ACChangeEnterBehavior = {
attach: function (context, settings) {
$('input.form-autocomplete', context).once('ac-change-enter-behavior', function() {
$(this).keypress(function(e) {
var ac = $('#autocomplete');
if (e.keyCode == 13 && typeof ac[0] != 'undefined') {
e.preventDefault();
ac.each(function () {
if(this.owner.selected == false){
this.owner.selectDown();
}
this.owner.hidePopup();
});
$(this).trigger('change');
}
});
});
}
};}(jQuery));", array('type' => 'inline'));
}
?>
Note: The idea is based on this discussion: http://drupal.stackexchange.com/questions/87439/drupal-7-disable-submit…