
Customizing Drupal - Part 6 Views
In the previous Tutorial, Customizing Drupal - Part 5 Content types, CCK and Taxonomy we learned how to create a new content type with customized fields. Now we want to create several customized views for viewing the content that we create. Enter, Views module. One of the most powerful modules in the Drupal Arsenal. Views allows us to create web pages that display our data any way we want them, sort the data, and show only certain types of data.
Head over to Administer-->Site Building-->Views. Now we get to see the power of views. Here you see the administer views page. You will notice that there are already some default views set up for you. Lets go ahead and click on the tab "add" so that we can add a new view for our new content.

The first thing we need to do is fill out some basic information about our view. Give your view a machine-readable name, which in my case I will call user_articles, which is what I want my view to display. This Name will only be viewed on the Views administration page, so make sure you name it something you will know on sight. Next, choose the roles you would like to have access to this view. By default, everyone can view the view, so if this is ok with you, you do not need to check any boxes. However, once you check one box, all other non-checked roles will be unable to veiw this custom view.
Finally, give the View a description, again this will only be viewed on the administration pages for views.
Next, we wnat to decide what type of View we want. Do we want this content to be viewed as a page, or a block, or both? I prefer to make my page views and block views seperate, so that I can change one and not effect the other. For now, lets just create a page view. I will show you a short cut for making the block view later.

First, lets put a check in "Provide page view" which indicatest to the module we will be presenting our data as a page.
Next, give the URL path to the new page, you do not need the entire URL such as http://www.linuxbasement.com/newview. Instead, just put ending of the path, which in our case is "user-articles". This will indicated to our site that this page can be viewed at http://www.linuxbasement.com/user-articles.
Next we must decide what type of view we want for our page. I will only be going over the most used view types, which is Full nodes, Teaser List, Tabe View, and List view. Feel free to experiment with the other views as well.
Full Nodes view will show the entire content of you nodes. In this case, it would show the entire articles, listed down the page, much like a blog. This is particlularly handy on blog sites and news sites.
Teaser list will show the title and author of the node, but will only show the first couple lines of text in the body, and give a "more" link to view the rest of the node. This is also handy for news sites.
Table View, which is what I will choose, shows a neat list of nodes with the content that you choose, such as author, date created, name of node, and so forth. More on this soon.
List View is much like the table view, but outputs the text in a different format. Experiment between them to find the one you like more.
In most cases, you want to make sure that "use Pager" is checked, so that if you have multiple pages of nodes, such as the linux basement front page, that it will create links to multiple pages so that people can go back and view old content.
You do not have to check "Breadcrub trail should not include home"
Finally, select how many nodes will be shown in our view. If you were using full nodes, you probably don't want more then 5 full nodes on a page. However, because I'm using the list view, I can fit much more in the viewing area, so i'm going to choose 30 nodes. Again, experiment as to what is best for your site. Keep in mind, once I have more then 30 nodes, a link to the "next page" will be at the bottom of my view so people can go back further because of that "use pager" check box.
Now that we have our page set up, we need to decide exactly what type of content is giong to be displayed. Lets take a close look at the Fields options.

Fields are one of the most important elements of Views. They are exactly what the user will see of your nodes. You go to the "add field" dropdown menu, and browse through the many fields that can be selected. Lets go ahead and select our fields:
Node title - this will show the title of the node. I will choose to make it "as link" so that the user can click on the title and view the entire node. If you chose, "sortable - yes" then you would be able to sort by alphabetical order.
Text: Article Type - This is one of the customized fields that we created in CCK. It is important to note that any content field that you created in CCK can be shown as a field in views, and sorted. This is a very important concept, because this means that anything you create in CCK can be dynamically displayed on your site with Views, which is why they are the powerful one-two punch of the drupal Arsenal.
Node: Author - This will give the authors name. I like to give it the label "created by" and make it sortable so people can see all of the articles by one author.
Created Time - This happens to be one of my favorite fields, because this enables you to sort in chronological order. This will show when the node was created, and you can allow anyone to sort by the time, to see the new or oldest node first. I also like to make this the "default Sort" as "decending" which will show the newest node at the top of the list.
When we've selected these fields, you want to make sure they each get a Label, which is what will show up on the actual view, over them when they are sorted. It will make more sense when you see the view itself, but just note you should make this very human readable. You can also adjust where they are by using the arrows to move them up and down the order. The top field will be listed first, and so on.
The next section we come to are Arguments. Here, we can choose to create and customize an RSS feed for this view. In our case, this would allow users to subcribe to user articles via a feed reader.

Make sure to put a user friendly Title, as this is what the user will see when they subscribe to the feed.
Next, are the all important filters. As of right now, we have selected to veiw many fields for nodes. However, we have not chosen what nodes we would like to see. Filters is how you can drill down to certain content types, which in our case, is User Article.
Lets add the filter Node: type, and then select User Article from the list. Please note that you could choose to show multiple types of nodes.

I also would like to filter by Article Type, which is the CCK field that we created. I will highlight all of the options here, and "expose" this filter. That way, when people go to the view, they see this exposed filter, and can click on what type of article they would like to view. Then they can sort by that type of content on the fly. THIS IS COOL. What this means is that any content type, or contenty field that you choose, can be sorted not only by you, but by the users, just by making the filter exposed. You should experiment with this, as this is one of the more powerful features of CCK and Views.
That should be all for now. We've already chosen what type of filters should be exposed, and what our default sort is, so we can safely hit "save". Now our View is ready! To test it out, you will have to manually navigate to the URL you set up. In our case it is http://www.linuxbasement.com/user-artclies. Once you've decided your view is the way you want it, you should add the path to one of your menus by going to Administer-->Site Building-->Menus, and adding the path "user-articles" to one of your menus. In my case I added it to primary links so that it shows up at the top of my page.
Once nice part about Views is the ability to "clone" them. If you go back to Administer-->site building-->views, you should see the option to "clone" next to your views. This is a great way to create a block view for the same content types. In other words, I can "clone" my user-articles view, and then rename it, deselect "page view" and select "block view". Now I should have a new block that presents a similar view.
The power of CCK and Views can be intimidating, but it can only be learned by experimenting and exploring the different options that are available to you. Soon, you will see that you can create anything from Personel databases to grocery lists. Please feel free to stop by the forums and ask questions.
