July 2015

Removing the entity ID from Drupal entity reference fields

On a site I am currently working on I have a content type that includes an entity reference field. When creating a node, the entity reference autocomplete includes the entity id in the field after selecting the entity desired. This can be confusing for the user.
When trying to find a solution to this problem I came across many issues and different proposed solutions including a sandbox module called Entity reference trim .

I tried the entity reference trim module but it has problems when the form was submitted and had errors.
Taking a closer look into this module I could see that it was altering the widget (adding a hidden field) and overriding some of the core Drupal js to do with the autocomplete functionality. While poking around the autocomplete javascript I noticed that the autocomplete fires a 'autocompleteValue' event.
Using this event I could change the entity reference field value and store it, then change it back when the form is submitted.

Below is the code that I am using but be aware that I have not yet tested it extensively and not tested it with multiple entity reference fields. Also, as the event bubbles, a more generic version could probably be coded.

NOTE: you will have to change the jQuery selector.

 (function ($) { 
Drupal.behaviors.damnId = {
attach: function (context, settings){
// Get the entity reference input
$eref = $('#edit-field-postcode-suburb-und-0-target-id', context);
if($eref.val()){
// If field has value on page load, change it.
var val = $eref.val();
var match = val.match(/\((.*?)\)$/);
$eref.data('real-value', val);
$eref.val(val.replace(match[0], ''));
}
// Listen for the
...

Drupal taxonomies pre configured and ready for import

Many times in the past I have wanted a taxonomy structure for things like car makes and models or Australian States and Suburbs and find myself recreating them every time. So today I decided its probably better to have them stored somewhere. Luckily the Taxonomy Manager module easily allows you to export and import taxonomy data easily. I know there are other options for importing and exporting taxonomies, but I found this one is probably the easiest.
Here is a few for you to use, and for my own use late :)

Simple list of car Makes and Models
 Abarth 
-500
-595
-695
-Other
Alfa Romeo
-147
-156
-159
-164
-166
-33
-75
-90
-Alfasud
-Alfetta
-Berlina
-Brera
-Giulia
-Giulietta
-GT
-GTV
-MITO
-Montreal
-Spider
-Sprint
-Other
Armstrong Siddeley
-Star Sapphire
-Other
Aston Martin
-AM
-DB4
-DB5
-DB6
-DB7
-DB9
-DBS
-Lagonda
-Rapide
-V12
-V8
-Vanquish
-Vantage
-Virage
-Volante
-Other
Audi
-100
-200
-5+5
-5E
-80
-90
-A1
-A3
-A4
-A5
-A6
-A7
-A8
-Allroad Quattro
-Cabriolet
-Fox
-Q3
-Q5
-Q7
-R8
-RS Q3
-RS4
-RS5
-RS6
-RS7
-S1
-S2
-S3
-S4
-S5
-S6
-S7
-S8
-SQ5
-TT
-V8 quattro
-Other
Austin
-1800
-A 40
-A 60
-A 99
-Freeway
-Kimberley
-Lancer
...