Drupal 7 - How To Use Views Contextual Filters

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this tutorial I will show you one way to use contextual filters in views in the next annals tutorial I will show you how to do the same thing but with panels so let's start by installing the views module so what I have done is to add some car brands and car modules so I have this mercedes-benz basic page and this Oddie basic page and actually let's add a menu item and give the content a path like this and save so now we have this path and this menu item and let's do the same for the mercedes-benz page like that so now what I want to do is to list all the car models for mercedes-benz down here and the same thing for Audie and I will first show the basic method of using just blocks and paths and then we add the contextual filter method and in my next panels tutorial I will do the same thing with panels so I have these all the modules here like OD Cabriolet and aerate so let's make a view that lists them called structure views at new view and let's call these models and we saw articles and we want to create a block and let's use a table continue and now we could at this place for both of those car brands and I have begged these car models like we have the tag over here so we can use that as a filter so let's change this to Audie and then add a filter here and select the Audie tag so now we should see only all the models here let's save this and now we could position that block below the main content here so let's find it and position it to the content area and configure it and let's use the Audie weights here like this so now if I go to the or debates I should see our view at the bottom here and there it is we have the models here and the brand information here and you would do this same thing for this Mercedes Benz to just clone them display so let's do that clone this and rename this and now you can see that if I would have a lot of these brands or some other items maybe hundreds you probably don't want to do 100 displays so this only works if we have a few number of these displays and that's why we are using the contextual filters next so let's finish this first we select the block here position it save and then we restrict it to this mercedes-benz bad and here we have the mercedes-benz models - okay that's all fine but let's use contextual filters for this first we need to add a taxonomy term for the page so let's edit the basic page and add a taxonomy field select term reference and here we go unlimited numbers so now we can tack these Bates's and use this title name as a tag or a brand name and and the same thing for the whole so now instead of doing multiple displays we could take this bag here and based on that tag we could list these models if all these articles have the same taxonomy term here so let's see our view again so we only need one display so let's first of all take of these talks and mature my filters here and delete this display and rename this as models and save this so now we have to get the taxonomy turn of the page and we can use a custom code to get it so first of all let's select has taxonomy term ID if we select provide default value then you can see that we can't really take the taxonomy term from the Bates if we were in the taxonomy Bates some taxonomy term Bates then we could get the taxonomy term from the URL using this option but there is no option to get the term from the Bates tax field so let's use a code for this and actually we need to enable the devel first okay actually it's enabled so I could use this code to load the node so I use node load function and then I get the first argument and the first argument is the node ID because if I edit this you can see we have the 44 here so if I paste this here now we get to the exactly same page as we would with the OD text so the arg1 gets the first argument which is the 44 and the node text here is the zero argument so exadata 44 is the second argument but this starts from 0 ok next we can use the DPM function to see what kind of variables we have available so let's save this and refresh the page and now we should see the note object here and here we could fetch the dag ID so I copy this and then we can use it here you must return the filter you want to use so we are returning the note tag ID actually let's use menu get object function instead it is recommended to use this menu get object function instead of the arc function so let's save this and refresh this page again so now we can see the models here because all these articles have this already tag and of course we can see the same thing in the Mercedes Benz page so refresh this and we are not seeing because we have still this restriction here in the block configuration so we want to show this in every page and also you want to show this block in basic Bates content types only of course you could have something like brand Bates but let's save it and this this out like this and here we can see the models so now you don't have to have multiple displays just one display in my next panels tutorial we will get the taxonomy term ID from the panel and send it to the view as an argument so then we don't have to have any blocks here because we position the view in a pain inside a panel but that's the topic for the next tutorial if you like this video please like subscribe share thanks for watching see you in the next tutorial bye
Info
Channel: Samuli Natri
Views: 16,983
Rating: 4.6559138 out of 5
Keywords: WDTutorials.com, wdtutorials, web, development, tutorials, tutorial, Web Development (Industry), video, instructional, drupal, drupal 6, drupal 7, drupal 8, cms, cmf, panels, contextual filter, views, router, menu_get_object
Id: u6zar2KcDwc
Channel Id: undefined
Length: 11min 5sec (665 seconds)
Published: Sun Apr 05 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.