Skip to main content

Language lessons: How can I fix my autocomplete box?

An article from ComputerMinds - Building with Drupal in the UK since 2005
10th Nov 2015

James Williams

Senior Developer
Hey, you seem to look at this article a lot! Why not Bookmark this article so you can find it easily in the future?

Languages make everything so complicated! I just discovered the autocomplete bit on my reference fields wasn't working - until I tried searching by another language, and then the results gave me a surprise:

Autocomplete results are not in the same language as their search input for entity reference fields

 

My homepage, originally in French, has had an English translation added, but whilst editing in English, I had to search with the French title in order for the page to come up... with the English title! Here's how to solve this...

My site uses Entity translation and the Title module (which I usually recommend as the best way of making your site translatable), and this field is an Entity Reference field. The field is configured to use the 'simple' selection method, which is the default.

Simple entity reference settings

Instead, I needed to use Views to build the list of content that can be referenced, to be used instead in that 'Mode' setting. Create a view, add an 'Entity Reference' display, and add the 'Field: title' field (use the search box to make this easier), instead of the 'Content: title' field that gets added normally. The following screenshots should help show how to do this:

Set up an Entity Reference display with the 'Field: title' field

This 'Field: title' field is the properly translatable field that the Title module sets up, whereas 'Content: title' is the original title that is not truly translatable. I also suggest adding the filter for 'title_field:language', filtering to the current user's language -- otherwise the autocomplete search will search across all languages! (So in my example, I could search for either "Homepage" or "Page d'accueil" to find the page.) By the way, if you want reference taxonomy terms, instead of using 'Field: title', you'll want 'Field: name' (and the name_field:language filter) instead. Some other entity types use other wording too.

Configure the format settings to use the new field

Next, configure the settings in the format section of the admin screen to use the new field for searching. Finally, once you've saved the view, edit the reference field and change that mode setting from using the simple method to instead use views, and pick the view you just created.

This now works, I can search for 'Homepage' and get 'Homepage' as a suggestion in the autocomplete, as I had originally expected! It's bizarre, but hopefully this discovery and solution might help someone else :-)

This also demonstrates how you can use views to search for other fields on content, not just their titles - this isn't just for translatable sites! You can even use a different combination of fields in the view to change what's displayed in the suggestion box (e.g. adding product codes, images, etc).

Hi, thanks for reading

ComputerMinds are the UK’s Drupal specialists with offices in Bristol and Coventry. We offer a range of Drupal services including Consultancy, Development, Training and Support. Whatever your Drupal problem, we can help.