Drupal 7 Creating a Membership directory with Views and Profile 2

By shane
Wed, 2012-11-07 18:15
comments
Daily Dose of Drupal Episode #46

Share with Others

Drupal is a powerful platform for building out various types of websites. In this example, we look at how Drupal 7 can be used to build out a very simple membership directory page that would be fitting for various types of social networking websites. In this example we use the Drupal 7 Views module and the Drupal 7 Profile 2 module for creating the directory view.

Also look at my previous Daily Dose of Drupal episode on the Profile 2 module. If you are new to views, you can check out our Drupal views introduction as well.

In this episode you will learn:

  • How to build a membership directory page with the Drupal 7 Views and Profile 2 modules
  • How to use relationships in a Drupal 7 view
  • How to use exposed filters in a Drupal 7 view

Welcome to another Daily Dose of Drupal, we’re on Episode Number 46 today. As always; I am Shane and you can follow me on Twitter @smthomas3 or you can hop over to Code Karate website and check out all the other things that we have going on. A while back on the Daily Dose of Drupal episode I believe it was on the 19th of September I did an episode on the Profile 2 Module and this is going to extend that a little bit and just show you how you can build a view … basically a membership directory using information from those fields that you created with the Profile 2 Module.

Just a quick overview if you don’t remember if you don’t watch that video in the Profile 2 Module will basically allow you to create a separate profile which can be a collection of fields, maybe it’s date of birth, biography, your social media profiles, things like that and have an actual separate profile that’s attached to your user account on a Drupal website.

So this is going to be useful for any type of membership website or any type of social networking website that’s using Drupal where you need to list the actual members of the site. So the first thing that I’m going to do is to make sure that I have Profile 2 and Views installed, which I do and well actually if you look at it let’s come to the modules page and let’s go ahead and confirm that I have the Profile 2 Module installed, Profile 2 is turned on and it looks like I do not have the Views Module.

So we’re going to go ahead and download the Views Module, so I’ll go ahead and use Drush from the command line to just download Views and we will come back into the modules page and use the filter to find and turn on Views and Views QI, so I’ll go ahead and save that and what we’re going to do in this example is we’re just going to create a very basic views page that lists all the members of the site and it also show their profile information if they have any.

So if we go ahead and look at the profile module or the Profile 2 Module we click on Configure, you’ll notice that we have two different profiles; we have a Main Profile and we have a Reviewer Profile. We’re only going to be concerned with the Main Profile here. All this profile has is a Full Name and a Bio Field.

So nothing too difficult, there’s a couple of fields. What we’re going to do now is we’re going to create this View and we’re going to make sure that it links to this Main Profile. Let’s go ahead and look at the users that we have on the site; so it looks like we have a couple of different users so we should have be good to go as far as some basic content for the view.

The View is not going to look at the end but it’s going to be workable and you’re going to be able to use that to build any type of view that you need. If you haven’t used Views before, this probably isn’t the starting point you want to look at, there’s probably a bunch of other tutorials you can go and learn the very basics. This one is going to show you how to build a basic … a fairly basic View but using a relationship. So I’m going to create a View called Site Members, we’re going to show Users and we’ll go ahead and show the newest first, that’s fine, we’ll create a page called Site Members, the path will be test3.codekarate.com/sitemembers and in this case we could show a table format if you wanted a listing and you wanted to be able to easily administer this or a grid format or on format if you just wanted the format with CSS.

I’m going to go ahead and use a grid and we’ll show 12 items and we will have it use a pager and go ahead and hit Continue and Edit. So the first thing we’re going to do is we’re going to add a couple of extra fields here; so we selected the fields and I’m going to go down to user and we already have the user name as you can see, I want to show the user create a date. If this was an administrative page you could show the user e-mail address and user edit link but depending on if you’re showing this to your average user if you want a site membership directory that any user can see, you probably don’t want to show any of those fields, the edit link would only show up of course if they have access to that but you don’t want any user to be able to see user’s e-mail addresses most likely.

So we could add user’s picture if they had one and we’ll go ahead and leave at that. I don’t believed any of the users on this site have a picture so we don’t have anything to show there but if you did have uploaded pictures you would of course see an image there.

So it be come down here, it shows a label of a picture of the user name and the created date, I’m going to go ahead and get rid of the label on the image even though there are no images, just imagine that there would be a user profile picture there if any of these users have actually uploaded the image and for the grid settings we can say maybe we only wanted to be 3 columns, so if we apply that you could see if 3 columns but what we really want is we want to show those extra fields that we have in our profile that we created with Profile 2. So this main profile we really want to show this Bio and this Full Name field.

Now your first thought would be … well let’s just try to add this field but since it’s actually a separate entity you can’t get to it from just a normal fields interface. You actually have to first add a relationship and this relationship is going to reference this original user entity to that profile entity that you created, in this case this Main Profile. So we go and we click on Add Relationships and we’re going to select User Profile and this shows up because of the Profile Module has been turned on, we’ll go ahead and click Apply and you could require this relationship.

What this will do is if any user does not have a profile, they will not show up in this listing, if you check it ... if you leave it uncheck then it will still show the user but these fields will be blank because it won’t actually link to anything, you can select which profile you want to use, in this case I’m only going to show the Main Profile and I’m going to click Apply.

Now that requires if you’re familiar with SQL or Databases it’s kind of like of inter joined versa left joined sequel statement if you’re not familiar with that then that may not quite make sense but basically all the views is just a very fancy user interface for making a specific types of database queries in structuring the results just a way to build simple list and of course it gets pretty complex when you get into the contextual filters and the relationships but what this will allow us to do is it will now allow us to add those fields that we wanted.

So you can see there’s another option here for profile, if you scroll down you can see those or I can just go ahead and limit it so I only see the Profile fields. So now I can select Bio and Full Name, go ahead and hit Apply, I can leave the label, you of course want to use the relationship that you created so you need that to show up there and I will leave … I’ll go ahead and remove the label from this one and then I can of course rearrange them however I want.

So user name, full name, created date and bio, now if I hit Apply and I scroll down I can see that this user of course they must not have profile, they have an old bio, this one has a bio that says Test User but they’re … and this is the user’s actual full name and down here here’s the full name and a bio as well.

So we can go ahead and save this, click on View Page and will take us to Site Members page and you can see I now have a grid of the site members of the website, it has my fields from the profile pulled in and you can do a whole bunch of cool things; you could add different types of filters so you can search for site members, you can do a whole bunch of stuff, so you can come in to the view, you can add a filter; for instance we’ll say full name, go ahead and hit Apply and we’ll go ahead and click Expose here, I’ll just a name, we’ll go ahead and put Member Name and we’re going to use contains … keep in mind this is depending on how many users you have, this could make it relatively slow search query but it will work and we’ll go ahead and hit Apply.

What we did here is we’re adding a filter and we’re exposing it so the user is going to be able to input what they want to search by. So I will show you how that works; so I’ll hit Save and now you can see there’s basically a search box so I can search by member name. If I go ahead and type User you can see that I get just this Test User because it’s searching by this field only, if I search for the first letters of Shane you can see I get this member.

Keep in mind it’s only searching by this field but you can add of course different dropdowns if you had all different types of options for various members and you could use the view to search and filter through these different members. So this is good for building out User Administration interfaces for you if the default user administration interface isn’t good enough for your needs or if you need to build out a membership directory for your other site users.

That’s really all we’re going to go over today, just some Views concepts that we went over here looking at Relationships and Exposed Filters and Views and also using that with the Profile 2 Module so you can help or start building out your membership directory. That’s it for this time on the Daily Dose of Drupal and we will be back again tomorrow with another exciting episode. Thanks for watching.