Creating a Drupal 7 Entity Reference View
Share with Others
The Entity Reference module not only allows you to reference entities within your Drupal site, it also allows you to do so using a custom created entity reference view. This allows you to leverage the power of the views module to control how the entity reference selection field is displayed.
Just getting started with the views module? Check out our Intro to Drupal views.
Hello everyone and welcome to another Daily Dose of Drupal. Today we’re going to be going over how to create an Entity Reference View. If you followed past episodes of the Daily Dose of Drupal you know we’ve covered the Entity Reference Module quite a bit and we’ve even had an episode where we covered how to use the Entity Reference Module to create relationships within Views.
Today we’re going to be actually creating an Entity Reference View and you’ll see a little bit what that does and why it might be useful. I am Shane Thomas, you can follow me on Twitter at smthomas3, also make sure to check out codekarate.com to check out all the videos and posts and learn more Drupal. So let’s go ahead and get started. I have a simple development site here with a couple of articles on it and we’re going to use the Entity Reference Module to just create a way to relate or reference related articles.
The first step is to make sure that we turn the Entity Reference Module on if it’s not already. As you can see we’re using 7.X-1.1 right now. So we’ll go ahead and save that. The next step is going to be to create our Entity Reference Field so I’m going to go to the article content type and go to Manage Fields. I will be adding a new Entity Reference Field and we’ll just call it Related Articles. We will go ahead and leave it at checkboxes and radio buttons so we can easily allow multiple articles to be referenced.
Here you can select a couple of different options. We want to be a node and we want the article to be the option we select here. And here’s the important part, for now you can originally and we’ll show you how this works. There’s two different modes, there’s the Simple Modes with the optional filter which is what we’ve used in the past and there’s a Views Filter by an Entity Reference View.
So let’s start with a simple mode and we’ll see how that looks and then we’ll come back and we’ll look at actually creating that Entity Reference View. So let’s go ahead and save this and now if we come back out here and we create a new article, you’ll see at the bottom I can select Related Articles. But actually we’re going to want that to be a checkbox instead of radio buttons there. So we’re going to go ahead and change that to allow multiple values. So we’ll go ahead and say that it can be an unlimited amount of values and we’ll save that. And now let’s look at creating an actual Entity Reference View.
This is useful if you want to control how the ordering and the display of this field actually shows up. So as you can see right now it’s just showing Test Article 1, Test Article 2, Test Article 3. We may want to sort this by various different things. So we can go ahead and use Entity Reference View to do that. So we’re going to go ahead and add a new view here and we will go ahead and select that we want to display content. Let’s call this Article Entity Reference. So let’s call this Article Entity Reference, we’ll display Content of Type Article.
We don’t want to create a page or a block. We’re actually going to be creating the Entity Reference View so we don’t need a page or a block. We’ll go ahead and click Continue and Edit. Now you can click Add here and you can add Entity Reference as an option. This will add the Entity Reference View. As you can see it shows all content that’s published and is a content type that is Article. You can sort by postdate or whole bunch of different things since it’s using the power of Views. So let’s just keep it simple and let’s just save it. You need to select Search Field.
So when you’re creating Entity Reference View you need to select the Search Field. So if you come in here you can select the various search field and this shows any field that you’ve added to the View so you can see there’s content title. And this is the fields that will be searched when using the autocomplete widget. So if you are using an autocomplete it will search through any fields that you check in this selection right here. So go ahead and use that, click Save and now we have our first Entity Reference View.
Let’s go ahead and just see how that shows up, we’ll come back into our content type, go to article manage fields, go to our related articles field and edit that and we’re going to change the mode now to Filter by an Entity Reference View. There’s only one Entity Reference View showing up and that’s the one we just created, Article Entity Reference. If you have any arguments that you need to pass in you can pass arguments directly into the view. In this case we’re not going to do that. So we click Save, now if we go to Add Content Article you will see that everything looks the same except now there’s a link here which actually links out to the Article and that’s because it’s actually using that View which has the links set up already. So let’s go ahead and take a look and see what else we can add here.
So we will go to our Views. Article Entity Reference View. If you click on Title you can see it’s because there’s this checkbox for link this field to the original piece of content. So if we uncheck that that link will go away. We may also want to change our sorting. So instead of sorting by postdate, let’s sort by title and let’s go ahead and go descending since I believe…we’ll start with ascending and see if that changes it. I don’t remember the order that was in so we’ll try that out. We could additional fields here, let’s try adding…let’s see if we have anything in here that we want to add. We can add the author user ID, you could link that or reference that to the actual author itself and use username or any other field that’s on this content type. There’s not a lot of fields.
This is just an article content type but you can see how you can use the power of Views to build this however you want. So let’s just show this for now and see this turns out. So we’ll save that. If we go into Add a new article now you can see not it says Test Article 1, Author you ID and Type Article. So you can see you can do a whole bunch of stuff within Views. If you’re familiar with Views you’ll know that you can pretty much change this to look however you want. You can then create articles and reference other articles within it.
So if I create a Test Article 4,we can upload image if I wanted to, I could pick articles that this is related to and I can click Save. I thought that was the title in the Tags field so we’ll try that, and there you go. So it works, you can then change…if you’ve used Entity Reference you know how to change the display, how this displays on the actual Node View page.
So you pretty much have the power of Views in your hands and you reference content with ease. Go ahead and give it a try, let me know if you have any questions about how it’s useful or how to get it to work and we’ll see you next time. Thanks for watching.