Drupal 7 Entity Reference Module

By shane
Thu, 2012-09-13 16:37
Daily Dose of Drupal Episode #7

Share with Others

Sometimes in Drupal, you need to be able to relate or reference content from within another piece of content. Lets say you are building an inventory management system and want to make sure to remember that Dave (a User) checked out the stapler (a Node or piece of content in the Drupal site)... Well the Entity Reference module makes this possible. There are many different use cases for this, so watch the video and learn how it will make your life easier.

In this episode you will learn:

  • How the Entity Reference Drupal module works and how it can be applied to a number of use cases
  • How the Entity Reference Drupal module can be used to relate two different types of Drupal nodes
  • How the Entity Reference Drupal module can be used to relate a Drupal user to a Drupal node

Welcome to another Daily Dose of Drupal, Drupal goers! And today we’re going to talk about the Drupal Entity Reference module. This is episode number 7 and this is brought to you by codekarate.com and we’ll get right into it today.
The Entity Reference module; you may be familiar with Node Reference or User Reference if you have used Drupal 6 and CCK in the past and this is fairly similar but what is going to allow us to do is reference different entities within our other entities of course.

So if we’re creating a new Node, we can reference other Nodes or perhaps the user or a group before using organic groups and we’ll go ahead and get started by downloading the Entity Reference module. So am in the Drupal 7 website right now; test.codekarate.com, I’m going to download the Entity Reference module and you can see it contains two modules; Entity Reference Behavior Example and Entity Reference.

We’re not going to worry about the example module; we’re just going to enable the Entity Reference module, it also requires Entities; so we can go ahead and tell Drush to download the Drupal 7 Entity module as well. So now it’s going to enable the Entity and the Entity Reference module.

So if you hop over to our site we go into our module’s page you’ll see that we have the Entity Reference module and the Entity API module as well. So now that we have that going we’re going to build off our example from yesterday a little bit which we have a content type called “Doctor Appointment “ and in here we’re going to add a new content type and we’re going to call this … I’m just going to call it “Room” and this will be a place where we can store different rooms for appointments and we’re going to keep it real simple and just get rid of the Body Field on the Room Content Type so it’s just the title, make it really easy, we’ll add a room; call it “Room 101” and we’ll go ahead and add one more; Room 102.
So now we have a few Room Content Types … a few Room Nodes I guess you could say and we’ll go into Structure back to Content Types into our Doctor Appointments and we’ll go Manage Fields and we’re going to add a new field; you’ll see there’s now one field type called Entity Reference, we’re going to reference a room for an Appointment, you can do an Auto Complete, a Select List or a Checkbox, we’re going to do a Select List in this case, scroll down to the bottom and hit Save and you get to select the Target Type; this is the Entity you’re going to be referencing so if we wanted to reference a user we can select Users so we could reference specific users and assign them to this specific appointment and we’ll do that here in a second. But for this case we’ll go ahead and use Node and the Node we’re going to be referencing is a Room, we select the Room Nodes.

You could use the simple option which is where you can select this down below, you could also use Views … looks like there’s an error there but you could also use Views to select different Entities based on the different types that you want and then use that to reference specific notes in this case.
So we’re not going to sort it, we’ll go ahead and save it from there and see if that works okay. So now when I go Add Content and I go to Doctor Appointment … something happened here; go ahead and debug this one quick, go back into out Content Type, we go to Doctor Appointment, Manage Fields, we have our Room here; I’m going to add this in Appointment Details and save it, so it’s listed under there, let’s go ahead and edit this, make sure we select Room as our Target Bundle, hit Save, now everything should be working, go ahead and add Content now, Doctor Appointment , if we go into Appointment Details and also at the bottom we can also select which room we want so we can select one; this Appointment is going to be … say it’s an hour long, it’s going to be the 17th in the evening and you could use a module such as Auto Node Titles to get rid of this Title Field which in some cases you’re going to need to do but in this case we’ll leave it … leave the title in there, go ahead and select a room, save it and now we have a simple Appointment for First Name, here’s the Date of the Appointment and here is the Room that it’s assigned to.

So pretty simple; we’ll go ahead and also reference a specific user so we will create a quick user; I only have one user right now and we’re going to say this is Doctor 1, I’ll go ahead and do that, give them just a password that they can then change and we can add a new role for a specific doctors if we wanted to, allow doctors to see different things but just to keep it simple we could notify the user if we wanted to … I’m not going to, I will create and you can see there’s now two users on this site. If I go back into Structure into Content Types and back to the Doctor Appointment I will then add another Entity Reference Field for Doctor and we will add a Select List on this one as well.

You know what we’ll just do an Auto Complete just to show the different options. We’re going to select that it’s going to be User and it will be simple, go ahead and save that, keep everything else the same, we can obviously change some of these values, add Help Text, change the size of the Text Field or set a default Doctor but we’ll go ahead and keep that the same, save it and now the next time … well first of all we’ll go ahead and drop this in there, rearrange it a little bit, save that.
Now go ahead and create one more Doctor Appointment; just call this Another Test. If we go into Appointment Details now you have the Start Date; say this is on the 19th at 8:30 to 9:30, it’s going to be in Room 102 and the Doctor should be working here … it took a little bit longer than I expected but it’s Doctor 1.
So if we go ahead and save this now you can see Room 102, Doctor 1 and you’ll noticed that as when you’re adding that, it puts this little 2 here and that 2 is actually the User ID of that specific user, so, so in this case this is the Doctor 1 is User ID 2. If you would come over here, you hover over the Edit you can see down at the bottom … over in here its user/2, so that’s the User ID of that user. Because you sometimes don’t want them to … or you don’t want certain people or you just want to hide the User ID to make it clean so you don’t have people asking or client’s asking, you can switch that over to a Select List depending on how many users or course you have on your site.

If you have a lot of users it might become unwieldy but we’ll go ahead and save that just to show what it’ll look like as a Select List, pretty basic, about as simple as it gets, pretty simple module but it allows you to do an incredibly large amount of things especially when you’re building out more than just one website and you’re really building out applications with Drupal such as a Booking System for appointments like this or maybe it’s a bug Tracking System and you need to assign a bug report to specific users. There’s just a whole bunch of used cases for it and tomorrow we’ll go over a different topic, go ahead and follow me on Twitter @smthomas3 and sign up for the Code Karate Newsletter to receive … I guess the ability to see longer videos in the future that I plan on posting. Until next time, this is the Daily Dose of Drupal, see you later.