Problem/Motivation
When typing a content title into a link field or a menu link, Drupal returns a list of 10 items that contain that phrase anywhere in the content title, sorted by creation date.
The bug happens when:
- you are searching for a node with an older creation date,
- you type the exact node title in the link field, and
- Drupal returns 10 newer pieces of content whose titles include that phrase.
Since the node you are searching for has an older creation date than the 10 results Drupal returns, there is no way to get Drupal to show the result that exactly matches what is typed into the link field.
For example, I was looking for the "Science Education" node, but I saw a lot of results that had "Science Education" as part of the title, but the node with an exact title ("Science Education") was omitted from autocomplete list, because the list was limited to 10 items and did not include the exact match expected to be shown first. See screenshot.
This issue may be an issue of any Drupal autocomplete field that reuses core functionality for the autocomplete. The link field implementation uses default settings of that autocomplete functionality.
Steps to reproduce
- Create one node whose title is exactly "About".
- Create another 10 nodes containing "about" somewhere in the title.
- Go to /admin/structure/menu/manage/main/add
- In the link field, type "about".
- Check autocomplete values in the list, the "About" node (the one you created in the 1st step) is omitted. (Because it would be the 11th item when sorted by creation date.)
Proposed resolution
Sort the link field's autocomplete results by title length (shorter titles first) before limiting the results to 10 items. This would ensure that exact title matches are always first and long node titles will eventually be found by typing more characters into the link field.
Something like:
SELECT * FROM `node` WHERE title LIKE '%about%' ORDER BY length(title) ASC;
See https://stackoverflow.com/questions/31315669/how-to-use-mysql-like-with-...
User interface changes
Instead of the link field's autocomplete showing a list of the 10 most recent nodes that match, the autocomplete list will show the 10 shortest titles of nodes that match.
Original report by dealancer
This bug happens in the link widget. And yes this is a bug, not a feature ;-)
When there is many content on the website and someone is searching for "some phrase" among existing content, Drupal does not give a preference to the content with an exact match. In other words, a node with "some phrase" title is not even showing in the list, but in this list there could be other content items with a title like "prefix some phrase suffix" that contain "some phrase", but they are not an exact match.
In the given example I was looking up for "Science Education" node, but I saw a lot of results that has "Science Education" as part of the title, but the node with an exact title ("Science Education") was omitted from autocomplete list, because a list was limited by 10 items only. However the problem is not in the length of the list, but in the search query as the exact match expected to be shown first. See screenshot.
This issue may be an issue of any Drupal autocomplete field that reuses core functionality for the autocomplete. The link field implementation uses default settings of that autocomplete functionality.
Proposed resolution
There are could be several solutions to this issue, but all of them would be altering SQL to add the order option, it could look something like this:
SELECT * FROM `table_name` WHERE col LIKE '%ami%' order by (col LIKE 'ami%') desc;
See https://stackoverflow.com/questions/31315669/how-to-use-mysql-like-with-... for more ways.