Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now we come to one of the hardest concepts to understand, relationships. As with Dynamic Filters I'll only be able to give a peek into the vast world of possibilities. To demonstrate, I'll show what amenities are available at the location where an employee works. That information is connected with the location because after all, a location would have a cafeteria, not an employee. But we want to display it in a list of employees to show what amenities are available to any given employee. To do that, I'll have to set up a relationship between the employee, and the location.
We started setting that connection up in the video Connecting two fields using references. At the beginning of that video, we installed the module references. If you haven't done so already, you'll need to write now. You can get it from drupal .org/project/references. Once we installed that module, we added a reference field so we could store locations in a Location content type, and then we pointed to those records when we wanted to show where an employee works. If we go back to our site, we can see that by going to Structure>Content types>Employee and manage fields.
We scroll down to that Location Field, we see that it's a Node Reference, and if we edit that field, we see that it references only those nodes that are of the Location content type. There is a problem with this though, that's a very limited sort of reference, it only carries over the title of those Location Nodes. So we can see, for example, that Francesca works at the Ventura orchard, but we can't see anything else about that orchard. That's true even if we start to add other text fields there.
I'm going to do that and add amenities to those locations. To do so, I go up to Structure and Content types and I'm going to add a field in my location content type for amenities. We'll call it Amenities. It will be simply a Text field and Save. We then as usual have a number of options, and in this case, I'm going to allow for an unlimited number of values. It's going to be one line after another of different amenities that people can have.
Save settings, and now we're ready. I'll very quickly add amenities to the three locations we have, we'll go through this very quickly. To find them, we filter by Location and there they are! I'll open each of these in a tab. In Oxnard, we'll say there is Parking, and a Cafeteria, and an Exercise room, and then Save. I'll add similar amenities to the Carpinteria Orchard and the Ventura orchard.
Now, we're ready to add that information to our employee view on the front page. We'll try the obvious way, but it's also the wrong way. To do that, we'll edit this view and add a field. The field will be Amenities. Now, notice that it says it only shows up in the Location content type, but we'll add it anyway and see what happens. We'll accept the defaults and then scroll down and see how it looks.
Well, it added a column, but there's nothing there. That's because we haven't set up a relationship between the two. That's what we'll do now. We'll go back up to the top and save just for safety. Then we'll go back to our View and edit the Relationships. Now, most of the relationships here are built into Drupal. For example, it assumes that there is going to be a relationship between the author of a post and the post itself. Similarly, there are relationships between comments on a node and the node itself.
The one that we want to add is this Location. So we click, and add, and then I'll simply click through and accept the defaults. Now if we scroll to the bottom, you see that doesn't actually change anything in our View at all, we still have this blank column. But we start to see the effects of that relationship when we go back, and add or alter fields. I go up to that Amenities field again and now we have this extra choice. We'll use that relationship that we just set up.
Scroll down and Apply it, and scroll down and now we have all the information that we wanted. Incidentally, Relationships work not only with fields but also with sorting and filtering. For example, we could create a filter that showed only those people who worked at a place that have Truck parking, and if we go up here, and add some Filter Criteria, let's just choose one at random, Amenities, and you see once again, we have that relationship right there.
I'll remove it since we're not actually going to do that in this View. Finally, I'll go up and Save. We return to our page, and it gives us exactly what we wanted. The hard part about relationships is knowing when they're needed, and also which direction they should go in. For example, let's say that we created a View of locations and we wanted to see a list of all the people who worked in each one. You'd need to set up a reverse relationship, something that's not possible with core Drupal. There are a few solutions to that reverse relationship problem and it's a field that's currently changing in Drupal.
So rather than recommend a particular solution, here are some resources that will show you how you can figure that out for yourself. One of them of course is the site we looked at earlier drupal.org/project/references. There is some information linked to this about that one-way and two-way and reverse relationship issue. Another project that's related to this is called NodeReferrer. It's at drupal.org/project/nodereferrer. Finally, there's a similar project called relation at drupal.org/project/relation.
With hard work on the part of the modules maintainers, I'm hoping that this will be settled by the time you watch this video. In the meantime, you can solve most bidirectional relationship problems through careful construction of your content types and your views. As usual, the drupal.org site is the place to get the latest news.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 100103 Viewers
56 Video lessons · 113140 Viewers
71 Video lessons · 82006 Viewers
131 Video lessons · 39344 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.