Entity Reference View Widget

By shane
Fri, 2014-10-24 08:14
Daily Dose of Drupal Episode #175

Share with Others

If you have ever built a site using the entity reference module, then the Entity Reference View Widget module is a module that you should know about. It isn't always necessary, but can be a lifesaver if you have a lot of referenced content that you need to sort through and select from on your Entity Reference fields. This is a good replacement for using a traditional autocomplete field for your entity reference fields..

In this lesson you will learn:

  • How to create an entity reference view widget view
  • How to prevent selected items from showing up in your view
  • How to add sort options and exposed filter to your view

Hello everyone and welcome to another Daily Dose of Drupal. Today we’re on Episode Number 175. I am Shane Thomas, you can follow me on Twitter at smthomas3. Also make sure to head over to codekarate.com, learn about the EBook, check out all the various posts about Drupal and learn how you can get your own Drush cheat sheet for free.

Today we’re going to be going over the Entity Reference View Widget Module. This is a really handy module and if you’ve watched other of my videos on the Entity Reference Module there’s a number of them and how the Entity Reference Module can be used to build out really complex functionality on Drupal websites. So this is just another module that adds on top of that and it works really well at allowing you to easily select related content or reference content.

So we’re going to go ahead and come over to our test site here and we’ll show you that I have the module installed. So if you see the Entity Reference Module is on and the Entity Reference View Widget Module is on. I also have two different contents we’re going to look at, one is called Conference Room and one is called Room Items. So essentially what we’re going to want to be able to do is when we create a Conference Room content type we want to select the Room Items that are going to be available in that Conference Room. So Room Items is a very simple content type which is a title and a body and we’ll go ahead and look at conference room which is the same thing right now which is the title and the body field.

We want to go ahead and add a reference field so we can select the various room items that are going to be in this Conference Room. And this can work of course in any type of related content, select Entity Reference as the field type and the important part here is under the widget section, select View. This becomes an option as soon as you have the Entity Reference View Widget Module enabled. So we’re going to click Save, we will leave the rest of these at the default here, click Save. And now you need to select the view.

I already have one I was testing earlier, however in most cases you might not have a view available here so you have to create one. So in order to create one we’re going to go into Views, make sure you have the Views Module on of course and the Views UI Module. So we’re going to add a new view, we’ll call this Room Items Entity Reference and we’re going to show content of type Room Items. We do not need to create page or a block here. So let’s go ahead and say we want to continue and edit. So let’s add an Entity Reference View Widget to our view and let’s just click Save to see how that works. So now we can refresh this page here and select the Room Items Entity Reference View.

We will leave all the rest of the defaults except we will change the number of values to Unlimited and click Save. Now of we want to add a Conference Room, we go to add Content Conference Room and you’ll see there’s a title, there’s a body and then there’s a Room Items section here. So there’s no items have been added but you can click Add Items and it launches this widget here where we know that this doesn’t work. You click on it and it doesn’t do what you’re expected to do. You can’t really actually add the items, you can click that and doesn’t work. So what we’re going to do is we’re going to make this work. So now that this is set up on the content type, it’s all on the view. So we have to configure the view correctly.

So the first thing that I typically do is change this from Show Rendered Entity to show fields. I also change the unformatted list to a Table which is easier to look at. So now we have our title field, let’s also add a few additional fields. The important field that you have to add is this Entity Reference View Widget checkbox. So we’re going to add that and we will move it to the top. Now let’s go ahead and save and see if it looks a little better. Should at least be usable at this point, so if we come into the Room Item Section click Add Items, you’ll see now we can select the various items as…looks like five items to select from, you click some of the items, you click submit.

Now if you close out, the items will show up as reference, you can uncheck it, if you click Add Items you can change these items using this view widget. You will notice that it kind of works but it doesn’t work really well because if you click Add Items we have Whiteboard and Projector B selected. It doesn’t show you anywhere that these items are already selected. So if we select Projector A it just adds that item to the list. So there’s a number of things we can do to clean this up.

The first thing we’re going to do is clean up the fields a little bit. So we’re going to remove the label here, we will add a label to the title field just to clean up that view. We will also add the post-date field and we’ll have add the updated Date Field. These are not necessary of course to get it to work, just going to make it a little easier to read and provide us a little additional information. It’ll also show that you can add any fields that you want to this view. So let’s say that the next step is going to become into our content type for Conference Room, go to Manage Fields and go to the Room Items Entity Reference Field.

There’s a checkbox here to past the selected entity IDs to the view. What this does is any items that you checked when you reopen that views widget is going to automatically be passed into the view. And while that’s important is because if you pass in the already checked items you can then remove those from the view so they can’t be selected multiple times. It’s also nice to close the window after you submit the items so you don’t have to click the little close box from the top corner. So we’re going to check those boxes.

Now we will come back tour view and this is where the important piece happens where you actually can filter out the already selected items. So in the advance section in your view you want to add a contextual filter. Since this is a content, this is a node, you want to find the node NID. If this were other types of content such as users you’d use the User ID whatever the ID field is. You want to add that contextual filter. In the More section there’s two checkboxes we need to check. We want to allow multiple values and we actually want to make sure that these items are included from the view.

Once we have that we should be about good to go so we will save. We will try to add a new Conference Room, when I click add items you can see we have a More Information here. We can select a couple of the items, click Submit and they automatically pull in here. If you go to Add Items you’ll notice now that there’s only three because it’s actually excluding any of the items that are listed here inside that view. So now they’ll only be two. If we do not want those anymore we simply check them and they’ll be back on this list.

You can also use this to select or unselect it all. So as you can see it’s very handy and one more thing you can do which is useful if you are going in searching a large amount of items. You can add some things to your view to make it work even better. Now you can make the table sortable so you can click and sort things. We can also add a filter to the view that you can actually use to search. So if you want to search title we will add a title filter and we want to expose this filter so it’s an exposed filter and we will say for the operator it contains and we will click Apply All Displays.

We’re going to change this Ajax to Yes and then click Save. I want to see if this is actually going to work here. Let’s go ahead and try to add some new contents so we’ll just start with blank. We’ll call this Room 1, we’ll leave the body empty and we’ll go to Add Items. So you’ll see we have some items here, if we want to search for projector you can see it limits down our items, you can select that, click Submit, you can also then come back in here and add some additional items. So as you can see if you had a ton of items to search through or you have a ton of reference content that you needed to sort through being able to add sortable columns to your table and also expose filters. It makes it a lot easier to do.

So that’s all we’re going to go over today. The Entity Reference View Widget Modules are really cool little modules so give it a try. Make sure to check out codekarate.com and let us know if you want to see any videos in the near future and we’ll see you next time. Thanks for watching.