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 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
Alfa Romeo
Armstrong Siddeley
-Star Sapphire
Aston Martin
-Allroad Quattro
-RS Q3
-V8 quattro
-A 40
-A 60
-A 99