Using Views with the Entity Reference module in Drupal 7

By shane
Wed, 2014-05-21 06:50
comments
Daily Dose of Drupal Episode #151

Share with Others

If you want to learn how to use Drupal 7 Entity Reference fields with the Views module you have come to the right place. I first covered the Drupal 7 Entity Reference module in the Daily Dose of Drupal Episode #7, but I never went into the details of how to use it in a view. In this episode, you will learn how to create a couple Entity Reference fields and use the Drupal 7 Views module to display the referenced/related content. This episode goes over the basics of how to use Views Relationships and Views Contextual Filters. The episode even covers adding an Entity Reference field as an exposed filter within the View and making it a Dropdown instead of a text box.

If you are new to views, you can check out our Drupal views introduction.

The video is a little long but very informative. You can use this technique to relate and display all different types of content. This is especially useful if you are trying to build a CRM (customer relationship management) system, a Bug/Issue tracking system, a project management system, and possibly even when building some complex social networking type sites. Anytime you need to relate content or users, to other content or users (or any other Drupal 7 entity type) on your site, this will help.

Hello everyone and welcome to another Daily Dose of Drupal. Today we’re on Episode Number 151 and we’re going to be going over how to use the Entity Reference Module With Views and this is a little more of a complex topic so we’ll try to take a little bit longer and if you look back at codekarate.com Daily Dose of Drupal Number 7 as where I first cover the first Entity Reference Module you might want to start there if you don’t know what the Entity Reference Module is.

I’m going to breeze through the setup of it but if you watched that you should get a little bit deeper understanding. As always you can follow me on Twitter @smthomas3, also go ahead and follow Adam Learing on Twitter as well and check out our new Facebook page and if you haven’t signed up for the newsletter go over to codekarate.com and sign up for the newsletter but let’s go ahead and get started.

So we’re going to start by making sure we have the Entity Reference Module installed on our Drupal Site which I already did. You’ll also need the Entity Module in order to install the Entity Reference Module and we’re going to be creating a really simple … I guess you could almost consider like a CRM type setup or CRM system or Customer Relationship Management System where if you look at this top section here we’re going to create a content type called Contact and we want to be able to assign this contact to a specific Drupal user.

In this case we’re going to consider the Drupal being a sales person and an organization or something like that. We also want the user to be able to assign to a specific team and if you haven’t seen a database diagram before, these little lines here basically mean it’s a one to many relationships.

So a contact or user maybe may have multiple contacts assigned to them, a team will have multiple users assigned to it. Another thing to keep in mind is Entity Reference With Views provides two way relationships. This means that if you … you’ll only need one field, one Entity Reference Field to link these and you can go both ways. So if you add a Field … an Entity Reference Field on the Contact Content Type you can then find from all the users that have been assigned to a specific contact or all the contacts for a specific user.

The relationship goes both ways. And so that’s just one thing to keep in mind. We’re going to go ahead and get started though and like I said we’re going to go over pretty quick through this. So the first step is we’re going to create our two Content Types and add our Entity Reference Fields in the necessary spots. So we’re going to go into Content Types, you can add a Content Type for Contact and we’re just going to leave everything else pretty much the same just to keep it simple.

We’re also going to add a Content Type for team so this sales person is going to be able to be assigned to a team. In our Contact Content Type we’re going to go to Manage Fields, find Entity Reference Field and we’re going to call it Sales Person and we’re going to have this just be a select list. If you have a lot of users on your site you’re going to want to use an Auto-complete and the thing to keep in mind is we are using … we are targeting the user … the Drupal User Entity.

You could use Node but in this case we’re going to go ahead and use User, you can target any Entity that Drupal knows about, we’re going to keep everything else pretty much to the default and it should be good there. We’re also going to then need to … so basically what that does is that creates this part of relationship or I guess right here, the user to the contacts.

So now we can tie a specific Sales Person to a specific Contact. We need to create this relationship, so we could create Entity Reference on the Team and allow it to select multiple values and you could then reference all of the sales people that are on that team or we can add an Entity Reference Field to the actual Drupal User object or User Entity and that will be able to create that relationship from that direction as well. So either way it could work. It’s more of just what makes the most sense for you.

So we’re going to go into our Account Settings and Manage Fields for the Drupal User Entity and we’re going to create a field called Team, it will be an Entity Reference Field, we’ll leave it a select list because that sales person can only be on one team but that also means that there could be multiple sales people that have been assigned to a specific team. We’re going to leave everything else or actually we’re going to come back in here. We only want them to be able to target Team Content Types so we’re using the Node Object because team is a Content Type for Node Entity and we’re only going to let them select Team.

Okay so now that we have that setup we’re going to add some quick content to the sites so I’m going to add a couple of teams called Team 1 and create one called Team 2 and we’re going to make sure we have a few users on this site so I’ll create one called Sales Person 1 and you’ll notice that there is no Entity Reference Field here when you’re registering the account.

We might want to assign to a specific team or we might want to change that so we’re going to go ahead and open this up, go back into our manage fields and we can edit this and we say Display in User registration Form. So now if we refresh this and we come down here you can see that I select what team this Sales Person is going to be on so the Sales Person 2 will also assigned to Team 1, so go ahead and create new account and we’re going to edit this first Sales Person and assign them to Team 1 as well and we’re also going to edit the Code Karate user and assign it to Team 2.

Okay we will now add some contacts or we’ll just give them a name and you can see that a contact can be assigned to a specific Sales Person, it does show anonymous here but I think we could get rid of that if we need to. So we’ll say this one is for Sales Person 1, this one is for Sales Person 1, this one is for Sales Person 2, this one will also be … oops I clicked on the wrong content type there, Contact. So what we need … I’m just adding a couple of quick contacts so we can see what will happen when we have Contact that are assigned to different users or different Sales People in this case. I will add Contact for myself, just my name. Okay so now we have a bunch of Contacts and a few Teams and this entire relationship has been set up.

Now as we’re actually getting into the Views part. So we’re going to go ahead and create two views to show how this is going to work. We want to create a view that will show basically all the contacts for a specific user so we can … if we are looking at this we can find for a certain sales person who all their contacts are. We also want to create a view that can be added to the team page that shows all the users or sales people that are on that team.

So we’re going to go ahead and start with that one. So we’re going to go to a Team page here and we want to block that’s going to show up somewhere on this team page that’s going to list all the users that are assigned to this Team. So we’re going to create a Block View in order to accomplish this, so we go into Views … Structure Views and we’re going to add a new View.

In this case we are going to be displaying … there’s a couple of different ways we could do this; so really Views are pretty flexible, we can make a couple of decisions here and essentially it just depends on how you want to set up your relationships. We’re going to go ahead and just say we call this Team Members and we can go ahead and show Users because we’re going to be displaying Users.

Like I said if you want to set up your relationships differently you could still select content here, it just depends on how you want … how you want it to work and we’re creating a block, we’ll go ahead and make this a table of Fields, 5 items per page, use a pager, that’s fine, we don’t have that many items so we’ll go ahead and click Continue and Edit. So it’s going to show the User Name, it’s also show say the User ID and show the E-mail of the Sales Person. We’ll add these fields, I’m going to rearrange them, put the User ID first.

Okay so this is how it’s going to look, as you can see it shows all of them. We want to make sure that it only shows when we add this to the Team page we only want it to show for the specific … for users that are actually assigned to that team. This is where you need to start looking into relationships and textual filters in Views. Relationships, that is what’s going to allow us to relate these different types of entities and display things based on how this relationship has been configured.

So we’re going to start by adding a relationship and keep in mind we are currently displaying if I come back here this part, the user but we need to limit the user based on this contact or excuse me in this case, this Team that the Users are going to be assigned to. So we need to create this relationship inside Views so go ahead and add relationship and we want to select a bridge to the Content Entity that is reference via Field Team because we want to create that relationship to a field team, so go ahead and hit apply and we can go ahead and require this relationship.

That way it will only show users that have that relationship set up. Which every user has that relationship set up right now so it’s still displays all three in our example. What we need to do now is we know that when we add this block to a team page it’s going to know what team … the page is currently showing.

So in order to get Views to know that we need to add a Contextual Filter. This is going to allow us to filter down our results based on the page that we’re currently going to be on. In this case we’re in the admin section so it’s not going to work but when we get to the actual Team page we will be able to get that to work. So if you search for content and ID we can select that. In our Relationships we want … we’re actually showing the node for Field Team, we want to select the relationship that we just created and here is where it gets a little tricky but when the filter value is not available we want to provide a default value and we want to default the value for the content ID from the URL.

This will basically tell it if you’re on the Team Node page use that Node to pass in through this Contextual Filter and you could add some validation criteria to make sure it’s only going to show or work on the team page, you don’t have to do that but that adds a little bit more restriction and as you can see now when we see our example down here it doesn’t work.

We should be able to test this though because if you come in here and find the Node ID of one of these teams you can find the Node ID by looking … if you look at the actual URL you can see that this is Node/8. So 8 is the Node ID of Team 1. So if we come back into our View and we put 8 here hopefully it will work, and it does. As you can see it shows our two sales people. If we go to our other team, Team 2 which you feel look is going to be Node 9, if we come back in here and put a 9 here for the Team 1 you can see it only shows the other user that’s assigned to that team.

Now what we need to do is save our View, come into the Block section and look for a view down here, view of team members and we will put this we’ll say I’ll just drop in a sidebar first so we can see how it looks right above the navigation. If we come back to Team 2 you can see it doesn’t format well but you can of course change that or put it in different block. You can see on Team 2 it only shows Team members for that specific team, if we come back to the homepage that block is not there, if I go to a contact page that block is not there, however if I come back and go to Team 2, we’re just on Team 2, let’s go back and try Team 1 and you can see it shows the two sales people that have been assigned to Team 1.

So there we go, that’s the first step. The next step is to create a View and in this case we’re going to make it a filterable view for a specific sales person. So then a sales person or anyone else that was in this company or had a login to the site could come in and see what all the contacts … who the contacts are and who they’re assigned to. So we’re going to go back into Views, Structure Views and we’re going to add a new View and in this case we’re going to call this Contacts, we’re going to show content of type Contact, leave all these the same, we can make this a table or any type of display format that we want, display a little more for the number of items, continue and edit and let’s go ahead and add an edit link, a delete link just so there’s a few more fields to look at.

So as you can see now it lists our Contacts with Edit link and a Delete link. We now need to add Filters so we can sort via specific sales person. So we want to make sure we only show Contacts that have been assigned to sales person so we need to add that relationship.

So we add a relationship and we want to select a bridge to the user entity that is reference via Field Sales Person. Go ahead and require this, if one of these Contacts didn’t have that field set they would then drop off this list if we click that require checkbox but we should be okay here.

The next step is going to be adding a Field for the specific user. So we could go ahead and add a field here and we could select Content Sales Person and we could expose this Filter and in this case we could select … we’ll just leave that right now and you can see this isn’t going to work the way we want to though because this is going to look for a specific Sales Person’s User ID and we’re not going to necessarily know that.

What we would really want is for this to be a dropdown here. So if we come back … we can do this, if we come back into Structure Content Types and go to our Contact Content Type, go to Manage Fields, find our Entity Reference Fields for Sales Person and we come down here and we say we want to render Views Filters as a Select List. This will allow us to change that Views Filter to a Select List.

Let’s go ahead and click Save, come back to our View, I’m going to go ahead and save this, yup that’s just an error, we’re going to go ahead and now just get rid of this field, we’re going to try this again. So we’re going to add a filter for Field Sales Person. Now you can see it shows names over here which is exactly what we want. We expose this, I’m going to call this Sales Person and we are going to go ahead and leave everything else at its default. We want to make sure it’s one of these and now you can see we have a dropdown here for the Sales Person and if we select which person it’s going to show different contacts for this Sales Person.

So let’s save this View and click here to actually go view the page and we have our working View. We can select the Sales Person and see the contacts that have been assigned to that specific person. If you’re building a bug tracking system or issue tracking system or you’re building a CRM like this or anything else where you need to build relationships between different Drupal Entities you’re going to need the Entity Reference Module and you’re going to need to learn how to use Views, add relationships and add Contextual Filters to those Views.

So that’s it for this episode, I know it’s a little long but hopefully you learned a lot and make sure to sign up for the newsletter, we have an e-book coming out soon so be ready for that and we’ll see you next time. Thanks for watching.