Drupal 7 Panels Module Contexts Relationships

By shane
Wed, 2013-04-03 23:59
Daily Dose of Drupal Episode #136

Share with Others

Another episode covering the Drupal 7 Panels module. This time we continue learning about Panels module contexts, but focus on using Panels module contexts relationships with an entity reference field.

In this episode you will learn:

  • How to set up and use Panels context relationships
  • How to include related node information from an entity reference field

Hello everyone and welcome to another Daily Dose of Drupal, today we’re on Episode Number 136 and we’re going to continue where we left off learning a little bit more about the Panels Module and this will probably one of the last Panels Module videos for at least a little while and we’re going to continue on learning a little bit more about Context and in this case we’re going to learn more about Relationships and how they can be used as a specific type of context.

We’ll show two different ways of how that can actually work. As always I’m Shane Thomas, you can follow me on Twitter at smthomas3, also make sure to go to codekarate.com and sign up for the newsletter if you haven’t already and also click this support link and go ahead and become a supporter if you’re interested so you can help make these videos happen on a daily basis.

So go ahead and check out the Panels Module and the other Daily Dose of Drupal videos on the Panels Module if you haven’t already to kind of get you up to the speed and we’ll go ahead and jump right in. We’re going to show two different ways of how this Panels Module page can work so if you come in to the Panels Module we have our page here which has a couple of different things that we’re going to take a look at.
Actually we’re going to go ahead and use a Node View page instead of the actual Panel page we’re working on last time but we’re going to go ahead and look at the content types here and I have a content type called “Company” and one called “Contact” and I’m using the Entity Reference Module which there’s also a Daily Dose of Drupal video on if you’re not familiar with that which essentially means we can set up a relationship between these two different content types.

So we have a company which let’s assume that we’re building a very simple Customer Relationship Management System or CRM System using Drupal. We have a company where we enter in a prospective client it has just company names, some notes and then company status, I’ll go ahead and move that up to make it a little cleaner and we could have many other fields, website, phone number, address, all those different types of things and essentially then we have a contact which is an individual person within this company.

So there is a specific company field which is Entity Reference Field which means that one company could have two or three or four or any number of contacts. So we have this entity reference here which is going to be used in our Panel page to create that relationship.

If we look in the content I added two companies; ABC Company and Test Company 1 and I do have an example contact here which I’m going to go ahead and reference an ABC Company here using the Entity Reference Field. I’m also going to add another contact … there we go and we’ll reference the test company for this user. So how this can work is we’re going to go ahead and create a Panel page for this specific contact but on this page here as you can see this one just has the user’s name. I’ll go ahead and a little bit more information just so we can see some information but on this page we also want some of the company information to be displayed.

As you can see none of the information is displayed right now so we can come in here and we could of course come to the Contact Manage Display fields and we could display the label with the link on it and all of that information and it would display the company information but what if we wanted to pull in additional information about this company?

Well one thing we could do is we could create this as a Panel page so the contact will have … maybe the contact information over here and the actual company information that this contact works for over in the right. So we can easily get back and forth and then of course we could use this on the company page as well and then when you’re on the company page perhaps we have the company information and then the list over here of all the contacts that are part of this company and the list over here would be a View, we’re not going to go over that part today but just know that we could create this as a Panel page and just drop a view in to a specific section.

You just create a view of the contacts for this specific company and you could drop it into the Panel page. We may be able to cover that in the future episode if people are having trouble of how to do that. So we’re going to go into the pages section where you come into Node View, we’re going to click Edit and you can see we already have this contact one set up from a previous example.

So here it says … we’re going to go ahead and get rid of a couple of these that are no longer used from our previous example, we’ll get rid of this search form as well. So right now it has their e-mail, their phone number, the notes and the comment form on the actual contact itself but what we really want to do is display some information about this user’s company so we could come in here and add content and come into node and we could say we want the field company here which is just going to give us either the Entity ID or the Full Rendered Entity. But in this case we only want specific fields from this company.

We only want …we’ll open up this company content type here, we’ll say we only want the status field and the company name, we don’t want the Full Rendered Entity and the status field is either just a prospective client or an active client, we could have other client status as well but in this case we’re only going to have two.

So the only way to do this then is to setup a Panels context which … remember from last time we added context to a Panel page based on either an argument or through just a specific node, you can of course use other things than that but what we really want to do in this one is we want to do a node relationship and we’ll find our Entity Reference Field which in this case node from node on node company field company and this field_company is coming from this content type contact, I go into manage fields you’ll see this is field company and that’s the Entity Reference Field. So that’s what I need to select in this list and now I select Add Relationship it’s going to say the node is the node being viewed and it’s going to go ahead and set up this relationship.
Now you can see this relationship shows up here, we can update and save this and you can see now there’s node from node company. Now when we come over into our content section … oops I never saved it after getting under these fields, go ahead and get rid of those and save it. Now if we come into this content section you’ll see that we have a few more options. So first of all we can still add the node title field, that’s no different.

The only difference is we can now select this node from node for this field company so that’s going to give us the title of the actual company itself and not just the title of the contact we’re viewing. We can also look in here and find that we’re looking for the Status Field and here it is, Field Company Status and we’ll go ahead and just leave all this as the default for now.

You can see there’s only one option or context to select from into this list and that’s the Relationship that we set up and that’s because of course the contact content type doesn’t have this field. The Contact Content Type does not … but if we go into the company it of course has this field company status. So this relationship links from the contact to the company and gives us access to the fields that are part of this company.
So now we go ahead and finish this and of course we could add the whole body or note section if we want it as well but we’ll keep it simple. Now I’ll go ahead and go update and save and now I’ll come back so the company page still looks the same however if I go into one of the contact pages we’ll say Joe Smith, you can see here is the e-mail for Joe Smith, the phone number, some notes for Joe Smith but the company name and the company status that Joe Smith is a part of is in this other section. So if I click to the ABC Company it shows the company that Joe Smith is a part of.

If I come into Jane Doe in this case you’ll see that this is Test Company 1 so it’s a different company, company has a different status. It could have a whole bunch of other company fields like the company website, other important information. I can click on the company and it brings me to the actual company page. Obviously you can see there’s a whole bunch of other things you can do here using these different relationships to set up context and this is what really make Drupal flexible so if you’re building more of a web application like a customer relationship management system or an inventory management system or something to that extent this can be very powerful and very helpful.

You can of course do other things like use Panels to display styles and things like that and we’ve went over some of that in the past but just know that it’s very flexible, it puts a lot of power in your hands and things that you can do and it’s very useful for when you’re building out various types of websites.

So that’s a good getting started with how to use the Relationships portion of context, go ahead and take it and run with it. If you have any questions definitely let me know and we’ll be back again next time with another episode of the Daily Dose of Drupal, see you later.