Introduction to the Views Tutorial Series
Hello and welcome to TronWebsiteDeveloper.com. I am P-Dawg, the TronWebsiteDeveloper specializing in Drupal, and welcome to the first video tutorial in this 10-part video tutorial series on the Views module. I'm pretty excited to be doing this. As you'll know if you visited my website, I've got a form there that people can provide feedback on my tutorials, and what I've gotten a lot of is people asking about Views, in one shape or form. So what I've done is decided to go ahead and create a comprehensive video tutorial series. It's going to be 10 video tutorials in total on Views, start to finish.
We're going to look at things like getting started with Views, creating a view, customizing your view, creating Pages, Blocks, attachments—you know, attachments are kind of hidden in Views. Then we're going to look at theming Views, we're going to get into relationships, we're going to look at contextual filters, going to go on to Advanced Views development. So you're going to look at hook_views_data, creating your own tables in there, your own fields. We're going to take that a step further and look at custom handlers. So we're going to do this for numeric strings, how we override those with our classes. We'll take a look at the Five-star module which does some pretty cool things. And then we'll look at cloning views, exporting views, importing views, if you want to transfer these to different sites, as well as our recap. We'll look at cool Views add-on modules.
So the reason why I'm doing this is because when I started out with Drupal, I had no idea what Views was, but I knew that every site essentially had Views. And the reason for that is because it makes your life so much easier. However, I couldn't find one resource out there that started Views start to finish. Even recently, I've been doing some development trying to do the hook_views side of things, custom module development, and I was still having trouble finding stuff on Views and how to use Views and take it to the next level. So I decided after a little bit of deliberation to just go ahead and do this. Still got a couple other series on the go, we're going to be doing Ubercart Series 2, but I wanted to get this out there and do them jointly.
What is the Drupal Views Module?
So, let's get started. First thing I'm going to do is talk about what Views actually is. I know I've done this in a couple other video tutorials, so you can fast forward this part if you already know and are familiar with views. But essentially what it does is it creates a graphical user interface that you can then query your database with. So we know if you've checked out my Ubercart tutorial series that Drupal is essentially database-driven. So you're going to have a bunch of information in a database and you've got to get that out and render it onto webpages. So that's essentially done through a bunch of queries through MySQL or using Postgres, but essentially they all come down to, you know, SELECT ... FROM ... WHERE ...
So you're going to select some piece of data from some table where some condition is met. And so if you were doing this programmatically, you'd constantly be writing these queries over and over again, and it would start getting very repetitive. So what ended up happening was Merlin of Chaos went ahead and created the Views module. And what that allows you to do is actually query your database from within the Drupal site itself. So you can build all these views, save them, and reuse them, change them, modify them, override them, and all within the Drupal site itself. So it just takes this repetitive nature of constantly developing these queries and makes it a lot easier to do with a module out of the box.
Installing Views and Dependencies
So that's what Views is. Looking at setting it up, what you're going to do is go over to drupal.org/project/views and this is the Views module itself. So I'm not going to actually go through downloading and installing it, but I'm running a Drupal 7 installation, so I would get the Drupal 7 version here, download the tar, unzip it, put it into my sites/all/modules folder, and then I would head back to my site and I would go and enable that. If you're not familiar with those steps, check out my first video tutorial, I believe it was my first video tutorial in the Ubercart series. If it's not the first, check out, it's probably two or three, where we actually go and we start downloading these video tutorials, downloading all the modules for that series. One of them is Views, so you can get an idea of what we do.
Once you actually go ahead and do that, you'll see that in your modules list here we're going to have Views. So you would click and enable Views, and you will need Views UI as well. Views has a dependency, so you'll also need Chaos Tools. So again, not hard to get, drupal.org/project/ctools. You're going to need the Chaos tool suite, again, Drupal 7 version. So I've got that, I've enabled it.
For the sake of this video tutorial series, I'll just give you a fair warning: I've gone ahead and deactivated the Overlay module as well as the Toolbar that comes with Drupal, and I've gone ahead and downloaded and installed the Admin module. So that's what you're seeing here. I think it's a little bit cleaner, I've used it more often, I'm more familiar with it.
Exploring the Views UI and Global Settings
So now that you have those installed, let's go and we'll check out actual Views itself. So you're going to go to Structure > Views and we'll just click on from there. And this is what the Views UI provides you. So if you don't have this, or you couldn't find your list here for Views, it's probably because you didn't enable the Views UI module, so go back and do that now. Out of the box, Views does provide you with a few different a few different quote-unquote "views." And so what those are are different, you know, queries essentially that create pages or blocks or whatnot. So these are all here.
In another video tutorial, we'll take a look at some of the stuff that comes with Views itself. But for the sake of this first video tutorial and getting you familiar with the Views module, we're going to take a look at the settings. To be honest, I didn't know this tab was here for the longest time. I just didn't pay attention to it until I needed to see the Views SQL query, and then I realized, I have a settings tab. So, I wanted to make you aware of this. Interesting thing here we have, so: show the filters on list of views. Once you create a lot of views you can actually tag them as, you know, custom, or if they're related to a certain project. This will allow you to filter that list, so I'll show you when we go back to it. This always show the master display. When we're creating views we can set up a master display, and then off of that, create a page, create a block, create an attachment. And so this will allow us to see that master view, or what used to be the default view in Drupal 6 and I guess anything before Views 3. You should always show the advanced display settings. So what that is is our contextual filters and our relationships. You'll see that those are minimized when we actually get into Views. We could enable that here so they're always there.
Not a lot that you need to know here, but the one thing I did want to draw your attention to is showing the SQL query. So this is neat because if you're, you know, learning Drupal, you're starting to learn PHP, SQL, you want to get into things, you can actually see the query that Views is developing and get an idea of what the SQL looks like if you ever wanted to do something yourself for whatever reason. You can also look at the performance statistics so you can know how well your site is performing for what type of information you're drawing in. And this can have some implications if your site is huge, so you might want to check that out. And as well, other queries can be ran during a view, so we want to go ahead and save these configurations.
And again, there's an Advanced tab when you're in settings. And so you can disable caching. So if you're actually doing development, which we'll be doing in, you know, the seventh or eighth video tutorial and we're looking at hook_views_data, we might want to turn off this caching so that we don't have to, if we make any changes, clear our caches and pull in that new information from our module. Also the debugging is helpful here, and you can see that we can actually use Devel module and use the watchdog. So when we're looking at our logs, again, this is pretty in-depth for our first video tutorial, but we'll be looking at our recent log messages when we're doing some development later on. But just drawing your attention to that.
Creating a New View: Initial Setup
So that's kind of the setup, the back end. Just quickly we'll take a look at adding a new view. And we're going to do something kind of, you know, right out of the box, nothing crazy, just get a feeling for what an an an Views provides. So we're going to click Add new view. And first thing that Views wants is a name, so we'll call this "Dog Images". I've gone ahead and uploaded a bunch of images. If you watch my other video tutorials, you know that Bailey and Suzie are frequently on our screencasts.
So our description is going to be just, um, just random photos of Bailey and Suzie. Right? So this description is just for myself so when we're looking at the views list, you'll see the description here and it just gives you an idea of what your specific view is at a high level.
So secondly, we're going to want to show content, files... it depends on what you're looking at here. Essentially what this is, when you go back to your SQL, you're going to be pulling in information from specific tables. So this is your base table. So when we say we want to pull in content, we're going to pull in things from the node table. We can relate other information, but that's essentially what we're going to be pulling in, is nodes. If we wanted to list all of our taxonomy terms, we would say taxonomy terms. Otherwise, if we were doing content and we were just trying to pull in taxonomy terms, we would get the taxonomy term for every piece of content that we have, and you'd get duplicates. So I have a taxonomy term for Bailey and a taxonomy term for Suzie. Rather than see Bailey and Suzie just once, because they're only one term that exists on my site, I would see them six, 10 times, however many times I've tagged content with their names.
So here we're just going to stick with Content, stick with the Node table. So of type, like I said, all I want to see is my Dog Pictures, and we'll do this as newest first. Right? So we have the opportunity to sort this. So you could look at unsorted, you could go oldest first, you could sort by the title of the actual piece of content, or the number of times that it's been viewed, right? For lack of a better term, what they refer to as hits.
Configuring a Page Display
Moving on from there, you have two options here out of the box: create a page or create a block. Essentially a page will be just like it sounds. You can go to twd.box7/ and then "dog-images" is a perfect one here. So we'll leave that. And then that will actually be your view accessible there. What the block does is you can go to your Structure > Blocks and actually place that block somewhere on your page within a region. If you're not familiar with what that is, again, check out the first couple video tutorials of my Ubercart video tutorial series which kind of go over the terminology for Drupal.
But my page title, so that's essentially going to be what the page you see here, so "Add new view" is the page title here. Mine is going to be "Dog Images". The path is going to be "dog-images".
Now the display format, we have a couple options here right out of the box from Views. So we can look at a grid, an HTML list... forget slideshow because that really shouldn't be there, it's an additional module that we'll look at way later... table, and then unformatted list. Obviously these are all theming functions that are available out of Drupal. So we'll just do a, I don't know, we'll look at a grid, I'll show you what that looks like. So we'll do of teasers, because we're not going to get into actually customizing this view in this video tutorial. And we'll do it without links, so a person can't add comments, they could add comments or without. So we'll do without, and they're going to have without comments.
Items to display: we'll display 10 at a time. And when it says use a pager, it'll display 10, you can go to page two and see the other 10, page three and see the other 10. And then we also have the opportunity to create a menu link and include an RSS feed into these. Right? So let's go ahead and we'll save and exit this.
Understanding Teaser Displays and Content Types
And now we've created our view. So you'll see here I've actually created five different pieces of content on this site. I've got Bailey & Suzie Plank, got Little Suzie, I've got Bailey Sprawled Out, Bailey Curled Up, and Bailey in Her House. And this is now available at dog-images. So if I'm at home, right, this is my homepage, I can now go to dog-images and I've got my content here that I can click on and actually go to the specific image that I'm looking at. Right? I can go back, I can go to Little Suzie.
Now because we did this based on teasers, what this is actually developing, or developed this view from, is if I go to my content type, my Dog Pictures, go to Manage Display, and I look at my Teaser, here's what we're seeing. All I see is, obviously I get the title, and then I'm getting the body. The label's hidden and it's trimming the length. So if I actually want to see the image there, I can go and add this image. And it's going to show me the original image which is going to be massive. So let's go ahead and we'll change that to a thumbnail. And that's actually based on another module that's preconfigured for image manipulation. So you have your Image Styles, that's where that's coming from, but again, beyond the scope of this tutorial. But I can go ahead and save this. Now if I go back to dog-images, you'll see that I've added the actual image there, and that's because we're using the teaser format in Views itself.
Conclusion and What's Next
In the next video tutorial, we'll actually take a look at creating our own customized view where we'll add a title, we'll look at the actual Views setup from itself. But we'll also look at the format. So if we wanted to change this to a table, we'll look at using our content but actual fields, so we'll add custom fields where we can, you know, pull in a whole bunch of different information. We'll also look at sorts and filters specifically.
So that's it for the first video tutorial. Again, we're going to have a lot to cover in this Views video tutorial series. I hope that you're familiar with Drupal because it will require some basic knowledge. If you don't have that basic knowledge, check out the first couple video tutorials in the Ubercart video tutorial series because they'll get you up to speed. And again, I hope this helps. Hopefully we'll see you in the next video tutorial series. And if there's anything that you're looking for out of this, leave a comment, let me know, because I've got the outline but you know, maybe we can work some things in there. So again, we'll see you in the next video tutorial. Thanks very much for watching.