Drupal 7 Views Module Tutorial 6 of 10 - Views Relationships and Contextual Filters

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Applause] [Music] hello and welcome back to the Tron website developer comm impd receive eternal website developer specializing in drupal and this is the sixth video tutorial in this ted part video series on views and the last video we took a look at theming views and now we get to look at the cool world of contextual filters and relationships pretty exciting stuff and also some pretty confusing stuff for new time our new drupal users so to start off we'll look at relationships and we'll dive back in where we left off so you remember we changed some theming for a view so we you know change the title here and what it actually look like we add some padding I removed some of the more extreme changes in CSS just to show you rather just to take it out so it's a little bit more readable but let's go in and actually change this up and we'll add some author information you remember we did this previously and all that we could pull in was the UID of a specific user we couldn't actually pull in the information of the user so I'll just show you what I mean by that so if I go ahead and I go to add I can go to content and I can choose the author UID and you'll see here if you need more fields then the UID add a Content author relationship so here I've got no ability to pick from user so I'm a little bit limited before I actually show you relationships let's take a step back and look at how relationships actually work and what they are so um I guess first thing to look at is the views module itself essentially views is a query builder so if you remember for using if we're writing our own SQL we'd be going to select from where right but views makes it a little bit easier and that gives us a graphical user is to do that so what we're actually doing when we pull in content is we're looking at the note table and we're specifically relying on DN ID so if we want to pull in information from the user table just going to add some space in here user table the primary key for the user table is actually the UID so we have a problem because these don't equal each other NID can't relate to the UID so what we have to do is introduce a relationship and so what the relationship does is it looks at our node table and we have n ID but we also have UID and then so the UID can then link up to the user table whereby UID is equal to u ID and then we can pull an information related to that so essentially you have your node table give your user table in the node table you're going to have an NID so the idea of your specific node you're also going to have the user ID of the user that created that that user ID is going to be linked to the primary key in the user table where you can then pull an information from the user you can't do that originally because an ie does not equal UID so let's head back over here and let's take a look at how that relationship works we're going to go ahead and click Add and we're going to choose content and here we've got content author so we'll go ahead and we'll add that and now this identifier is going to be the author so it's whoever the author the the content is and we're going to require this relationship every piece of content that we have is going to have an author so we don't have to require this because there always be one if you have a piece of content so let's say it's a taxonomy term you could have nodes that have taxonomy terms or don't have taxonomy terms if they didn't have a taxonomy term you should require this relationship that node without a taxonomy term would not be shown so that's kind of what this relates to so we'll go ahead and we're going to actually override this just so it applies to our new page and actually I should cancel this and create in your display sorry bear with me go ahead and hit add actually no we won't that'll be the next step I'm sorry for confusing this so again we're going to go back into content author will hit override so just displays to all dog images and now we can go ahead and click add and if we go to filter we see we now have user so we wanted user and we have all this information that we can pull in from the user so let's go ahead and add the user name so this will give us the actual author who's uploaded the image and you'll see here we have the ability to choose the relationship there's only one so that's why it's automatically selected we'll create the label and we'll say this is the author name and we're not gonna put a colon because it will place one and we can link it to the actual user so go ahead and hit apply now when we save this and we scroll down you'll see here we've got author named Ringo author named Paul author named Peter often in your Wars all the authors that have added this we now have their name and we can link to their specific their profile so that's kind of how relationships work at a high level hopefully that that helps answer some questions I've got a lot of questions about relationships but they're pretty straightforward essentially just remember in terms of your select statement or rather your your SQL the the relationship is related to the from so from you actually would be inserting here your relationship where the you know node table the UID is equal to user table UID so that's where that pops in it's in the front statement where as contextual filters actually dynamically are created through for the where statement but I'll come back to that one we actually go ahead and we do that one last thing that I'll show you here is if we go to configuration people account manage you'll see that I've added a favorite dog name to to a user I can then now go ahead and go add field and I can pull in that users favorite dog of course we look for a dog nope favorite dog name go ahead and hit apply right and so it goes ahead and adds that apply that as well so for you know more sophisticated views you can go ahead if you pull in user information and a whole bunch of profile information and then pull it in by linking the UID to whatever piece of content you're looking at right so now I can see that favorite dog name is Bo fever dub name is Dexter favor dub name is Dumbo and so on so that's relationships they're pretty straightforward I find you know I had a tough time wrapping my head around them and then you know I saw a couple video tutorials myself from Drupal camps where they were where they explicitly explained it like this essentially views as your your query builder and relationships kind of plug in to the from part of your query so for me that really helped solidify the understanding of relationships and so hopefully it'll do the same for you they're pretty powerful you can definitely take this well beyond where we are but now we'll look at kind of the contextual filters so as I mentioned contextual filters are kind of your where statement so the filters that we have here published equals yes content type you've both dog pictures these don't change they're always going to stay as we always want to see published pictures we always want to see dog pictures and so views can automatically plot those in as a where statement you know where type is is equal to sorry this should be where type is equal to dog image right but what we want to do now is we want to create a view for our users to see what dogs that they've uploaded what images they've uploaded and I can't add that as a filter because I never know what user is going to actually be looking at that that's where contextual filter comes into play the contextual meaning the context that the view is being displayed so what we can do here is views will dynamically pull in some value so what we're going to actually use is the UID and is equal to and it'll use a placeholder right and that placeholder is something that we will give it and you can do that in a variety of ways so let's go ahead and look at that so right now I'm just going to go ahead and add a display and I'm going to rename this as so it's a page display so I'm going to say dogs uploaded by user so go ahead and apply that so now we've got this and what we're going to do is we're going to actually add this on to the existing path for all users which is obviously the user slash and then a % because that's going to be the argument that they receive and it will be my images then go ahead and apply that and what we'd like to see happen and I'll just show you briefly is we want this tacked on as an existing menu tab when a user goes to my account so it'll go to my account and then we want to have my images right here so we can do that by adding a menu tab and so this is my images this is part of the user menu go ahead and apply that now if we save this we can go we'll reload my account and I'll head back over to views here and just see now we have my images but when we click on it we see all the images right we see yours we see Peter we see Paul we see Ringo we don't just see specifically the images logged in by the user so let's go ahead and add our contextual filter and when you do this you have a whole host of options right and so the programmatic side of all these modules when they tie into views or view views comes with a number of set defaults so like the note table and that kind of thing it declares what you can use as an argument so you could install some modules that say that I interviews but they don't offer arguments for you know specific fields or whatnot that could be a problem it might just be something you want flag so if you're looking for contributing module arguments that might be the reason why you don't have them but here what we want to do is we actually want to use the user right we know we're looking for their UID so we can scroll down and here's the user ID some of the most common ones that you'll use will be UID an ID may be a comment ID I've seen a couple other cases where what did we use location ID right as an argument so using the location module so the a couple couple different options now once you actually add this there's the ability to relate it to the relationship that's existing so we'll leave that for now and what we want to do here is when the value when the filter value is not in the URL so there are a couple options here you can provide a default argument to the contextual filter or you can make sure that it's always in the URL or you can display summary if it's not in the URL bunch of different things here right so if our filter is not in the URL we know that somebody's not looking at profile page so they're not looking at user slash a user ID you slash my images so what we want to do is display a page not found right because essentially they're they're not on the right page so we don't want to do that so if they put in a user slash you know what is this page slash my images we know that not proper so we don't want to show anything so we can go ahead when the filter is is in the URL or default is provided we can go ahead and we can override the title the reason why we want to do that is you know if we weren't using the UID here but we're using the user's name we could go ahead and say you know Peters images right we could also override the breadcrumbs so it's dynamically we'll use the argument that you have and it will populate a piece of content being the title the breadcrumb so that it's specific to what you're looking at lastly the specified validation criteria so essentially this validation criteria it allows you to check the argument and make sure that it meets certain standards so those standards could be you know for a user ID that we're using is this user of a specific role so if we only want logged in users or sort registered users to see dog images you know by user on the site we can restrict that here so in terms of basic validation we can look at the content we can look at numeric PHP code we want to look at the user right and so we're only going to allow numeric you IDs so it's not going to work if it's user slash Peter slash my images right it's only going to work if it's user / 1 / my image we can also restrict it based on role what we were talking about so we'll go ahead and we'll authenticate the user to make sure that they're actually logged in and if it doesn't actually validate so if it's not a numeric UID or they're not logged in it'll show a page not found and so we can go ahead and hit apply and this actually will cancel that we're going to apply this to this page only and now when we go ahead and save this we'll reload our dog images here and you'll see here now we've got often in yours often in yours often in your scoff that name your worst often in yours all of my specific images and if I log in as Ringo let's say Ringo is looking at them sees the same thing if he goes to his account goes to my images Ringo's got his one dog image same can be said for all the other users they can go into Peter Paul and all they'll see is this view which is their specific images that's where it's a dynamic filter right so we're filtering the content based on the argument that's provided right here now let's say that we want to abstract this so we want this tab to be available for all users once they're logged in so rather than it be a tab here where it's my images we want it to be a universal so it'll just be your images right here beside dog images right so let's go ahead and change that up and I'll show you how we're going to provide a default value so this is something that you'll also be doing a little bit more frequently so we're going to change this path and we're not again as the super admins or going to change that in case you're curious how I'm switching back and forth that's the devel module that's the switch user block so I'm going to go ahead and go users um and I'm going to add this as my or rather user images right so we'll go ahead and apply that make this a normal tab you remember we covered this off in another earlier video tutorial we're going to put this in I want to sing its main menu but we'll have to chest I doubt so we'll go ahead and hit apply let's just make sure that that shows up properly so if we reload this page now we have my images right but it's showing everybody again which is a bit of a problem because we're not looking for a specific user here so what we need to do is here this UID we're going to actually when the filter is not in the in the URL obviously this display it also this should have been show page not found I'll show you what that actually looks like we'll save that I'm going to reload this page see we should have a page not found right because we don't have the actual argument in the URL here so that's why we're getting that page not found so that obviously works properly so what we need to do is we need to provide a default value and so you have a bunch of different options here the Content ID from the URL that's to get the note ID so if we're using a node as an argument you could use that to automatically pull in the node ID you could use a fixed value so it could be always be something you know maybe it's all if you're looking at taxonomy terms or something like that PHP code allows you to actually write in some PHP and return a value that will be the argument so that's pretty handy and again a raw value from the URL I've never used that I don't think you would ever want to use that but then again you have the taxonomy term ID from the URL so again we're talking about taxonomy so you can provide a default for that and then you can get the user ID from the URL right which is what we talked about from before if you're on a user page but what we're looking for is the user ID from the logged in user so what this will do is it will look at who's actually browsing the site and pull in their UID and use that as an argument so we're going to provide that default here so that's we're getting but then what we want to do is you want to make sure to specify some validation criteria right because if that user doesn't have a value if their anonymous user we might not want to show this so we can do a user and we're going to make sure that you know we can allow both you know that doesn't really matter right because we're providing a default but we want to restrict this based on a user role and we only want this to be for authenticated users and if they don't if they're not a fun to get users it doesn't validate they're going to get a page not found a little bit of an ugly situation there you don't want you know you're not amis users different page not found we could do no results found and then we could you know add an empty message or whatnot or just display a summary but we'll do page not found for now typically what we would end up doing here is you know for restricting it we would just dues or permission for restrict that to you know certain user types but just for argument's sake here we'll leave that as the validation criteria we'll go ahead and save this now when we reload our page we can see that because I'm logged in as yours my images is pulling in all of the worst images but if I change this and I'm not changing the actual page I'm just logging in as Ringo Ringo sees one image same thing for Paul Paul logs in looks at the same page it's all user images but all that he sees is his specific image because it's dynamically pulling in the UID from the user that's logged in so this UID is being pulled in from who's ever looking at it and this is being added to the where clause right so now it's restricting our our return value from SQL based on a dynamic value and that value is being pulled in from the user ID this could be any dynamic value you could so that's really relationships and contextual filters in a nutshell just headed back over to contextual filters just one you through some of the more common ones so obviously we talked about the user ID sometimes you want to use the the note ID so you know if you have a block that pulls in specific information for a note if you have them tied together there may be a node reference I do have reducer on that you can use that as an argument some of the other ones you know maybe if you have a really active site you can use the actual date you know today's date as a you know content thing so it'll show your most recent you can also do that if you're doing your comments listing use today's date show all the comments from today know what ID comment ID user ID those are pretty much the most common that I've used I'm sure there are lots of other weapons out there if you do have one or any questions please let me know and then in terms of relationships I mean author relationship is often pretty pretty common you also have a node relationship right if you can relate things on the node you know locations sometimes if you're using location module I think those are the most common that I've that I've run across but I'm sure there are plenty more out there especially if you're using other modules like a flag module or use a relationship that kind of thing you can introduce a number of different relationships taxonomy is always a key one you know use comes with its own taxonomy override so I hope this helped I hope it was pretty straightforward contextual filters and relationships are pretty complex if you have any questions post them make sure to put them on the site or you can always post them on YouTube I monitor both and again if these video tutorials are helping you please throw a thumbs up it goes a long way I love seeing those and I love seeing the comments that you know tell me how much these are helping out it's good to have that feedback so I know where to keep the videos going tempo and what we're covering so again I appreciate your viewing and we'll see you in video tutorial number seven of ten
Info
Channel: Web Development Tutorials
Views: 41,054
Rating: 4.9704795 out of 5
Keywords: drupal, views, drupal how to, drupal tutorial, views module, views module tutorial
Id: mRWtpcUm9pI
Channel Id: undefined
Length: 21min 31sec (1291 seconds)
Published: Wed Jan 30 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.