Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Extending views with relationships

From: Drupal 7: Reporting and Visualizing Data

Video: Extending views with relationships

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.

Extending views with relationships

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.

Show transcript

This video is part of

Image for Drupal 7: Reporting and Visualizing Data
Drupal 7: Reporting and Visualizing Data

44 video lessons · 6769 viewers

Tom Geller
Author

 
Expand all | Collapse all
  1. 13m 50s
    1. Welcome
      50s
    2. What you need to know
      4m 49s
    3. Using the exercise files
      8m 11s
  2. 12m 58s
    1. A yoga studio
      3m 18s
    2. A national organization
      2m 51s
    3. An entertainment company
      4m 3s
    4. An organization with hyperlocal branches
      2m 46s
  3. 40m 49s
    1. Planning your site's data structure
      3m 31s
    2. Creating record templates
      5m 40s
    3. Understanding Drupal 7's data-template model (entities)
      4m 27s
    4. Creating categories using two methods
      6m 14s
    5. Creating unusual field types
      6m 38s
    6. Reusing and grouping fields
      6m 25s
    7. Looking at Drupal's database
      3m 56s
    8. Migrating custom fields from Drupal 6
      3m 58s
  4. 20m 46s
    1. Connecting two fields using references
      5m 35s
    2. Importing data using Feeds
      8m 48s
    3. Changing the appearances of fields
      6m 23s
  5. 53m 15s
    1. Understanding why views are useful
      5m 1s
    2. Learning from built-in views
      5m 52s
    3. Creating and deleting a simple view
      5m 41s
    4. Diving into the Views interface
      8m 48s
    5. Surveying field types
      6m 16s
    6. Adjusting the settings of individual fields
      5m 31s
    7. Sorting and filtering data
      8m 11s
    8. Changing Views' administrative options
      7m 55s
  6. 33m 33s
    1. Varying how a view appears in different displays
      10m 10s
    2. Formatting views with grid, list, table, and jump menu appearances
      6m 43s
    3. Rewriting field output for interesting results
      4m 21s
    4. Creating multipart views using attachments
      6m 8s
    5. Styling the output of views
      6m 11s
  7. 33m 56s
    1. Importing and exporting views
      6m 21s
    2. Controlling access to data
      6m 44s
    3. Adding dynamic filters to views
      5m 41s
    4. Extending views with relationships
      6m 24s
    5. Improving data administration with Views Bulk Operations
      8m 46s
  8. 29m 43s
    1. Understanding locational data
      4m 43s
    2. Setting up the Location module
      9m 42s
    3. Displaying basic maps with the GMap module
      8m 37s
    4. Displaying custom maps
      6m 41s
  9. 23m 50s
    1. Exporting data in simple text form
      7m 4s
    2. Adding dates and calendars
      8m 32s
    3. Displaying content in a slideshow
      8m 14s
  10. 1m 20s
    1. Next steps
      1m 20s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Drupal 7: Reporting and Visualizing Data.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.