I found this issue in the Core issue queue today:
https://drupal.org/node/2146649

This issue belongs in the issue queue for the Devel project, not Drupal Core:
https://drupal.org/project/devel

So I went to "Update this issue" and started typing "Devel". I was presented with a list of several projects, none of which was the "Devel" project. I also tried just typing Devel and hitting enter. Nothing worked. I could not set the project of this issue to "Devel".

My suggestion would be in the auto-complete list to sort by the length of the results, or at least alphabetically. This would show the Devel project first, and then if you kept typing you could find the "Development" or other projects. Otherwise, if Devel doesn't show up when you type "Devel", you are out of luck because that is the end of the project name and there is no way to make it show up.

Comments

tvn’s picture

Priority: Critical » Major
Issue tags: +Drupal.org 7.1

Temporary workaround for this is to type "Project name (project nid)", so for that issue: Devel (3236).

jhodgdon’s picture

Thanks, that worked.

dww’s picture

The basic problem is that EntityReference selection plugins let you configure an operator for the autocomplete search to be either "contains" or "starts with". They all try to do a single query, with the right operator, and the only sorting options are to use other fields, not "relevancy".

The way to get exact matches to show up first in the list (what you'd expect) would be to be doing 2 queries each time. The 1st would query for an exact match, the 2nd would do whatever you configured it to do (starts with vs. contains). In theory, the first query should be relatively fast, but it's hard to know, especially on a site like d.o where {node} has 1.1 million records. If we were to go down this path, it'd be wise to run some EXPLAIN queries to make sure things don't get out of hand.

Given that, there are two basic approaches to fixing this:

A) Try to get a patch upstream into EntityReference itself to do this double query business (I haven't even searched the EntityReference queue yet to see if this has already been proposed).

B) Write our own custom selection plugin that does what we want.

We already have a "Project behavior" plugin (so instead of having to manually select all the bundles you care about, you can just say: "reference any node from any type that's configured to be a project" or "any node type that's configured to be an issue", etc). That's in project/plugins/entityreference_selection/Project_SelectionHandler_ProjectBehavior.class.php if anyone wants to take a look. We'd have to have that class implement getReferencableEntities() to do the direct match query first, then invoke the parent's copy of that function and merge the results.

I can work on this if necessary, but this seems like an issue that just about anyone familiar with EntityReference could fix, and perhaps the best fix is upstream in EntityReference itself. Step 0 should probably be searching if this is already being proposed/discussed in ER.

Thanks,
-Derek

dww’s picture

Okay, just spent a little while looking at the ER queue, and the closest I could find was this:

#1841924: Improving Autocomplete Widget Results Popup through better sorting.

The proposed solution there won't work for us at all.

It'd be great to get feedback from any of the ER maintainers here (Amitaibu, damz, bojanz or amateescu) . If someone sees any of them on IRC, please ping them. ;)

Cheers,
-Derek

jhodgdon’s picture

Should we just file a new issue in Entity Reference rather than hoping they'll notice this issue?

tvn’s picture

Title: Cannot change Project to "Devel" on an issue » Cannot change Project to a short project name on an issue
dww’s picture

@jhodgdon - #5: Yes, we probably want a separate issue in the ER queue to get their attention on this. Either that, or just move this issue there.

@tvn - #6: I'm not sure that's really the problem (although that'd be another way to help this -- searching for matches for the project machine_name field, not just the title). As I understand it, the problem is that if your field is configured to search using "contains" not "starts with", the project name/title you're searching for is short, and lots of other projects have that same string in their title/name, there's nothing to put exact matches first in the list of autocomplete options returned by the callback. So I think the "short project name" stuff is a bit of a red-herring, and the real issue is about project names that contain commonly-used strings...

jhodgdon’s picture

Title: Cannot change Project to a short project name on an issue » Cannot change Project on an issue if target project name is a substring of other project names

RE #6/7 - yes, more accurately, the problem is with trying to select a project name that happens to be a substring of other project names. If you are trying to change to a project with a longer name, it might be annoying but at least you can just keep typing until your actual desired project is there in the list. But if you are trying to change to "Devel", you type "Devel" (which is the entire project name) and the target project does not appear in the list.

Changing the field to "starts with" matching will not necessarily solve the problem. It could still occur for instance with "Views" -- there are a ton of projects whose names start with "Views", so you could conceivably have trouble there as well, unless the output is sorted either by length or alphabetically.

tvn’s picture

It'd be great to get feedback from any of the ER maintainers here (Amitaibu, damz, bojanz or amateescu) . If someone sees any of them on IRC, please ping them. ;)

I left them messages with Druplicon, let's see if they get them soon.

Damien Tournoud’s picture

It has been suggested by @jhodgdon already, but autocomplete widgets work best in general if you sort the matches alphabetically.

Could that solve the problem on Drupal.org?

jhodgdon’s picture

I think it would work fine sorted alphabetically, as long as that means case-insensitive and that something like "Views" comes before "Views Slideshow" for instance.

tvn’s picture

amateescu asked for a dev site to work on this, http://projectname-drupal.redesign.devdrupal.org/ will be ready in ~ 1.5 hours.

amateescu’s picture

Status: Active » Needs review

Thanks for the dev site. I made the changes there so everyone can go in and test it. Also posted a patch here: #2188111: Update the 'field_project' field with better matching and selection settings.

jhodgdon’s picture

I just tested the dev site that amateescu set up, and it is working fine. The autocomplete results are now sorted alphabetically, so if you start typing the beginning of a project name, you are guaranteed to eventually get the project you are looking for.

However, the matching is now "Starts with" -- which I agree is probably the only way to get this to work for the use case of "I cannot change the project to 'Devel'" that I originally reported. So if we deploy this, I think we will need to add help to the field to explain this.

Also, I think amateescu needs to make some deployment instructions for this change, right? It looks like the patch just adds extra options for the field, but also those options need to be selected.

amateescu’s picture

@jhodgdon, there's an update function included in the patch :)

drumm’s picture

Status: Needs review » Fixed

Deployed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.