Glad you could stop by the Linux Basement site. Linux Basement is an informational Podcast about Linux, open source software and lots of other wonderful technology. If you want to find out more about open source technologies, subscribe and have a listen!

#linuxbasement is up at irc.freenode.net

MP3 Feed
Ogg Vorbis Feed
MP3 Feed (all episodes)
Ogg Feed (all episodes)

Video blog (not the show!)

 

Drupal 6 Dynamic Content - CCK, Views, and Panels

Drupal 6 Dynamic Content - CCK, Views, and Panels

The Mission:
To create a site that sports a sortable and search able knowledge base, FAQ, Video feed, and podcast.

Drupal 6 Modules Used:
cck
getid3
panels
views
advanced_help
dhtml_menu
imce
pathauto
audio
fckeditor
link
token

Main focus of this tutorial:
Content type creation and CCK
Sorting the content via Views
Presenting the Views with a panel

The Goods:

In this tutorial, I'm going to assume that you've read my other Drupal Tutorials, and you already have a base site set up with all the goods, such as a theme you like, site configuration completed, clean ULRs and pathauto configured. If not, you may want to go back an look those tutorials over. This tutorial will focus on creating new content types, then sorting the content types, and presenting them in a aesthentically pleasing fashion.

For the purposes of this tutorial, I will be creating a knowledgebase and some media content. Keep a close eye on the kowledge base, because you can take that theory, and apply it to just about anything. Creating content types with different fields, and then sorting them, are how you get dynamic content in Drupal. I've created User articles databases, asset inventory databases, and more, all using this method. To truly use the potential of Drupal, you must know CCK and Views.

If you have five index cards on a table, and you want to sort those cards, how will you tell them apart? You give them labels. This is also how we will distinguish between content types in Drupal. If you do not give the cards different names, you will not be able to tell them apart. If you do not give your content types in drupal different names, then the Views module will not be able to sort by name. Similarly, we can get into finer grain control, by putting subcategories, or different fields, such as date, location, or anything you want, as an additional field in your content types.

Lets go ahead and create the Knowledge Base Content type. Navigate to Administer --> Content Mangement --> Content types. Then click the tab "Add Content Type".

Now, fill out the new content type name and system type, and give it an administrative description. We've done this before in tutorials, so I wont go over this part in detail, but look through the settings and set it up how you would like it.

In my screenshot you will see I'm using Organic groups. You will not have this on your installation.
 

When you are satisfied with the settings, hit "Save Content Type".

Now in the list of content types, you should see Knowledge Base. Up until now, we've been using the basic features of drupal. Now lets use CCK to create custom fields within our new Content Type by clicking on "manage fields".

What I want to do, is create a check box that says FAQ. If the user creates and article, and they believe it should also be included in the FAQ, then they just check this box, and it automatically becomes part of the site's FAQ. Why would I do this? Many times, knowlege base articles have highly technical, or specialized information. This is what a knowledge base is for. However, sometimes they have more gerneral knowlege, that often comes up in frequently asked questions, such as, "what is open source?" In this case, it should be in the knowlege base AND the FAQ. With the dynamic flow of CCK and views, I can save the user some time by adding this check box.

Back to "manage fields" for the knowlege base content type. You will see "New Field" and then a place to Label the field, give it a system field name (used in the mysql tables) what type of data you are using and the form element you would like to use. We will use the following, and click "save".

Once you click save, you will be brought to the next page where you can customize some of the settings for your new field.

Notice putting in help text is very important. This will inform the user what the checkbox is for, which in this case is "Check this box if you would like to include this article in the FAQ".

Notice the other values here. You may need them if you are creating custom field types. Go ahead and save when you are satisfied.

Now, rinse and repeat, except this time, I'm giong to create a field called "Category", again, make it a text field, but with a drop down menu. Then I will list the categories that the knowlege base article can fall under. This will allow the user to later sort the knowledge base articles by category. In my case, my site is about free and open software, so i will put
Google Apps
Openoffice
Moodle
Drupal
Koha
Asterisk
Wordpress
Wiki
Joomla
Sakai
Linux
Open Source
Other

In the allowed values list. I am also going to make this field "required" so that a user MUST choose a category when they create a knowledge base article.

 

The last thing I'm going to do with my Knowledge base is to sort the fields the way I want the user to see them. In drupal 6, its as easy as dragging and dropping them in the order you want, then click save.

Now if you go to the admin block and click on "Create content" you will see we have a new content type in there called Knowledge Base (I actually called mine knowledge base article). When you click on it, you should see something similar to this.

Go ahead and make a few mock knowledge base articles so that we have content to work with. You will need to see some content so that we can work with the Views Module. Also Check the FAQ box on at least one so we can test out the FAQ view.

Now that we have a new content type, lets use views to sort the content and present it in page and block formatting that will fit our needs. To do this, go to administer-->site building-->views. Even if you have not yet built a view for your site, you will see a list of views here that come with other modules, and have not been enabled. However, we want to create our view from scratch.

Click on "add" to create a new custom View. Go ahead and name your view knowledge base, or whatever will let you remember this is associated with the knowledge base content type.

Lets take a look at the customized "page" view that I've created. When you get to this screen, you need to go to the dropdown and select "page" then click add display. This has now created a page that will display your content. However, we need to define what content it will display, and how it will display it. Lets go through the settings one by one.

Basic Settings:

Name - This is just the system name of the "page" you are creating. You can leave it as page, or change it to knowledge base page. Just make sure you keep page in there somewhere so you know what type of view it is later.

Title = This is what will show up at the top of the view.

Style - There are several choices here. I am going to choose list, but you should experiment with these, and see how they format the page. This is how you can have tabled entries, and also teasers of full nodes.

Row Style - Further defines the view. I am going to choose Node and teaser, which is appropriate for knowledge base entries because it will show the first few lines of text after the question, and then a "more" link to read the entire article.

Items per page - Define how many nodes will show up on 1 page.

Page Settings: specific to the page display.

Path - This is important, what you enter here will be the path of the view you are creating. If I put kb here, then the view will be accessible at http://mysite.com/kb

Fields:

Although this is empty for us, this is an important configuration block to take note of. If you ever use the table or list layout, you can specify what fields to include, such as title, or in our case, category. You can also add things like the date the node was created here, which will then show up in the list on the view.


Sort Criteria:

Here you can choose how the data in your view is sorted. This is the default sort, there are also additional ways to allow users to sort by the fields you choose to show, but you will have to mess around with the Fields arguments to see this.

Node:Post date desc - This will show the nodes by date, in descending order.

Filters:

Right now, the view does not know that we just want to show knowledge base articles, because we never specified. If we were to go to this page right now, it would be showing all of the content for the entire site. This is where filters come in. You specify to filter by certain types of content, or date ranges, or fields, ect.

Node: Type = Knowledge Base Article - This allows us to ONLY show knowledge base articles in the view.

Node: Published True - This ensures that the "published" check mark must be there before things show up in this view. This is nice because you can go back and work on an article until it is perfect, then hit "publish" and it will show up.

That's it. Remember to save your changes while you are creating the view. I press save every so often just to make sure it all takes. Now to see if it worked, just navigate to the page view you set up http://yoursite.com/kb

If all went well, you should see a list of the articles you created.

Now, go back to the view again and edit it. This time, go to the drop down menu and choose "block". You will see that all of the field populated for page, now populate block content. This is extremely handy! You now have a block view for knowledge base articles also. You should however change items per page to 5 or even 3 for the block, or it will get flooded. You may also want to experiment with table and list views to get the block looking nicer.

So that's cool. We now have a page of knowledge base articles that is searchable through drupals default search. You can also begin experimenting using filters, and allow the user to sort by the category field we set up earlier, so that they can only see articles pertaining to a certain subject.

But what about that FAQ checkmark we talked about earlier? Lets create a new view, just for FAQ articles. Luckily this is going to be just like the view we just created, with a minor change, so instead of creating it from scratch click on views again, and then click on "clone" next to our Knowlede base view. This will clone the view, and you can rename it to FAQ. Then we need to change some fields. How cool is that? This really streamlines creating  a lot of views.

So what are the differences here now that we've cloned the view and renamed it? Well, you will see I renamed the PATH under page settings. This of course, is essential so that you now access this at http://mysite.com/faq

However, the most important thing to note here is the Filters. I've added:

Content: FAQ not empty

Remember that check box that we added before? Well, now it comes into play, whenever someone checks that box when creating a knowledge base article, the field will then become "not empty" and will be automatically added to this FAQ view. How cool is that?

This document is a work in progress. I hope to add more later.