Drupal 7 Entity Reference Prepopulate module

By shane
Wed, 2013-01-30 23:50
Daily Dose of Drupal Episode #98

Share with Others

The Drupal 7 Entity Reference Prepopulate module allows you to automatically populate an Entity Reference Field from the URL. This is similar to a Drupal 6 module called Nodereference URL widget, but works with all entities.

In this episode you will learn:

  • How to configure the Entity Reference Prepopulate module
  • How to construct the URL to get the Entity Reference Field to fill in automatically

Hello everyone and welcome to another Daily Dose of Drupal. Today we’re on Episode number 98, getting very close to 100. As always I’m Shane Thomas, you can follow me on Twitter at smthomas3; you can also go over to codekarate.com and sign up for the newsletter and check out the other things on the website.

Today we’re going over a very simple module that has some specific uses depending on what type of website you’re building. It is the Entity Reference Pre-Populate Module. In the past episode of the Daily Dose of Drupal I went over the Entity Reference Module so you can go ahead and search for that and look at that before you watch this one if you haven’t already or if you’re not sure what the Entity Reference Module is.

It basically allows you to reference different entities so you could have for instance a Client Management System which is what I’m going to kind of use as a rough example in this case in which you have a client company with multiple contacts at the company. So you have two different types of nodes as you can see on our test site here I have a content type for client which is going to be similar to a business and contact.
So a client could have multiple contacts and every time you add a new contact you reference which client you’re actually a part of. So if you take a look, the client is simply just a blank content type which had a title right now, you could put other things in there, there’s a body field as well.

The content or contact content type has just a contact name which is the title, a body field, a phone number. You could put multiple things here, e-mail address, whatever you need to collect about the … whatever type of information you need to collect about that contact and it has this client field which you can see as an Entity Reference Field. So what I’m going to do is I’m going to first mention what the Entity Reference Pre-Populate module does.

Basically this lets you pre-populate this entity reference field so if I go in and I want to add a new contact for a client I can say this is Jane Doe, I can add some information about Jane Doe, give her phone number and then I can say which client this contact is a part of. So as you can see it references a client content type that I already have so I created a node called ABC Company, I can save this and now I have a contacts that references this client.

But what I really want to do is let’s say I want to add a client or excuse me a contact but when I add this contact I want to make sure that this is already filled in, maybe even hidden because you want … you don’t necessarily want to have to look to client so ideally what I’d like to do is go to a client page and somewhere on this client page to be able to actually click a link that would then allow me to create a contact for this specific client and the Entity Reference Pre-Populate module only does a small piece of that.

It doesn’t necessarily give you the link; you have to do that yourself either through using the panels module or display suite module or some custom code. You can use any of those options and you can easily use tokens to generate a link but we’re going to just show you how it would work with the Entity Reference Pre-Populate Module.

So I’m going to go ahead and turn this module on so we can at least see what it allows you to do. So I’m going to go ahead and find it in the list, I will turn it on and save it and now I will come back to my content type and I’m going to go to the Contact Content Type, the one with the Entity Reference Field, as you can see here I’ll click Edit and there will be a new checkbox here called Entity Reference Pre-Populate.
It will default to uncheck so you can go ahead and check it. Now you get to basically say what you want to happen. So first is the action to take one pre-populate in the field with values so you can first have it do nothing which basically means it’s going to just show you the field with the text in it.

You can have it to completely hide the field or you can have it actually have the field disabled. I’ll go ahead and select Disable and you can force it to apply when you edit it so you won’t build to change that actual client but I’m going to go ahead and not do that so you’ll make it so you can edit the specific client that a contact is a part of.

Fallback behavior; you can set an error, redirect, hide the field or do nothing and this is basically what happens if you try to create that piece of content without the URL setup correctly. You can also set who can skip the access permission so you can basically set it and it will not be affected by this fallback behavior. You can also use it with the Oriented Groups Module but we’re not going to go over that.

But basically what this allows you to do is now that I set that I can of course since I have a fallback behavior to do nothing I can still create a contacts normally just like this and everything works, I can still reference the client company as normal but what I want to do is I want to come up here and I’m actually going to start by going to /node/add/contact.

So this is going to give me just to the basic page still everything’s still the same but what I’m going to be able to do is I can put question mark the name of the field which in this case I’ll show you where but says field_client=and then the node ID of that actual client. In this case it’s node ID of 6.

So now that I set that you can see if I scroll down it automatically pre-populates this field with this Node ID which is 6 and it’s said it to be disabled. If would have set it to hidden this field won’t be here, I could have set it to do nothing and it would be just pre-populated with this text and it would be editable.

So now I can of course put a test contact in, put a phone number in, that might be too many digits but that’s okay and I will save it and it automatically created that link to that client company for me. So now it’s reference correctly to ABC Company for the client for this specific contact. So as you can see it’s a simple module, it doesn’t give you the link out of the box but you can easily create it if you’re using anything with actually changing how this node displays.

Just simply add a link using the Panels Module and some tokens or I guess they’re consider like contact substitutions in panels depending on how you look at it and it’s also available pretty easily using Display Suite. So go ahead and take a look at this module and use it on sites that you need to link references or entities together very easily and just to show you quickly where I got that field name if you’re not familiar.

If I come into the contact content type, you can see the machine name or the field name was field_client. So that’s how I got that part of the URL, it’s just simply node/add/whatever the machine name is or your actual content type. In this case it was just contact then it’s the question mark to create the query string on the URL and it’s simply the field name which in this case is field_client and then the actual node that you want to reference.

If I try to reference something else it’s not going to necessarily work. You can see it doesn’t populate it because I’m only allowing client nodes to be in here. I change this to a 6, come back because node ID of 6 is an actual client content type or client node, it works. So go ahead and try this out when you’re needing to build complex structures using the entity reference module, it will make things easier you can easily populate values using the URL.

You will of course need to do more either some custom development or use some other display modifying modules in order to get it to work easily but you can go … I’ll leave that as an exercise for you to try out. Just go ahead and let me know if you have any questions and as always thank you for watching.