Week 11: Drupal CCK & Views (7/21/09)
Week 9 Instructions: Drupal CCK & Views
Videos
Assignment
- No assignment, other than to finish your final project by July 30th and send me a snapshot or screencast of your drupal site.
- Use of views & cck in your drupal site is NOT required! It is a bit much to learn at this late point in the semester, but hopefully you'll get enough of a taste of using views and cck in case you ever want to use drupal for a real website.
Notes
This week I'll walk you through creating an event calendar for your drupal site.
This involves the modules CCK, Views, Calendar, and Date, which you should already have installed and enabled (we enabled all the modules a couple of weeks ago).
Content Types
As we've learned, content is entered into a drupal site is called nodes. Each node of content has a name or title (like a blog entry title), and a body (the content of a blog entry for example). There are also different types of nodes (content types), such as blog entries, news, pages, webforms, etc.
With the CCK module (content construction kit), you can create your own new content type.
For example you might create a new type called 'job' for job entries, or 'course' for course descriptions. In our case we'll create a new content type called 'event' for calendar events.
First, go to site configuration->date and time and set the timezone for your server and any other settings there that you wish to change.
Then, to create a new content type, go to content management->content types->add content type: http://localhost:8082/acquia-drupal/admin/content/types/add
For the name, put "Event" (no quotes), and for the type, put "event" (also no quotes). You can add a description if you wish like "Use this to post events to our calendar".
The other settings below the description can be altered if you wish, like if you'd like people to be able to comment on events and so forth.
Under "Workflow settings" you probably want to uncheck "promoted to front page" so that when an event is posted it doesn't show up in your front page news feed.
CCK Fields
Like I mentioned, every type of node has a name (or title) and also a body.
But, you can also add extra fields to a content type. These are like extra columns in a spreadsheet (with each node on a row).
For example with our 'event' content type, we'd want a date field, where people can specify the date of the event, and also perhaps other fields, like a 'location' field where people can say where the event takes place, and perhaps a 'type' field where people can say if this event is a meeting, a class, a social event, etc.
After you've created your new 'event' content type and see it displayed in the list of all content types (http://localhost:8082/acquia-drupal/admin/content/types/list ), click the 'manage fields' link next to the event type to add new fields: http://localhost:8082/acquia-drupal/admin/content/node-type/event/fields
Scroll down and under 'New Field' enter Date for the label of the field and 'date' (no quotes) after 'field_" for the machine-readable field name.
Select 'Date' for the 'type of date to store' and 'Text field with date popup and repeat options' for the 'form element to edit data'. This is saying that this field is for entering dates, and you've specified how people can enter the dates (i.e. by typing in the date, or selecting from a popup, or using a popup calendar widget).
Click the 'Save' button and you'll be taken to a page where you configure the settings for that 'Date' field. For the default value of the 'from' date, you might put 'now', and then select 'relative' for the 'To' date. Expand the 'Customize Default Value' section and put "+1 hour" (no quotes) as the default value for the to date.
Then scroll down to "Global settings" and check that this field is required. Anytime someone posts an event, they have to specify the date of that event, it isn't optional.
I also set the 'Repeat display' to 'Collapsed'. This lets people specify if an event for example repeats every week, but collapsed means hide that stuff by default since it isn't used most of the time.
Finally click 'Save Field Settings'.
In the video I also added a 'new field' called Location (field_location) of type Text with a textfield entry box, so people can say where the event will be located.
Enter an Event
Let's test out your new content type.
Go to content management->create content->Event and fill out the form to add an event.
When you save that form, it will create a page displaying the event, with the time and location if you added those fields.
Views
The next component in this lesson is views. Views lets you display a set of nodes in various ways. For example with events you might display it as a block showing a list of upcoming events, or a page showing a calendar with the events on it, or perhaps a table with columns for name and date and location.
Go to Site Building->Views to see some of the built-in views that come with the views module. You have to click the 'enable' link on the right side to enable some of the views, such as 'tracker' and yes, there is already a built-in 'calendar' view.
Calendar View
After you've enabled the calendar view and checked it out (click the link next to the "path:"), you might notice that it displays everything, every content type, not just 'event' nodes.
Let's edit the calendar view to filter it to only show 'event' nodes.
The video is better than these notes at showing you how to do this, but basically you:
Click the 'Edit' link at the top of the calendar (or next to the calendar in the list of views), and click the + symbol next to filters, select Groups->Node and scroll down to Node->Type and click add. Then create a filter such that the view only displays node types that are one of type "event".
Click save to save the view.
Upcoming Events Block
Lastly, let's add a new view from scratch, that will display upcoming events in a table that is displayed in a block (see the front page of http://itls.usu.edu/ on the right side for an example).
Again the video is better at showing you how to do this.
Go to site building->views and click the 'Add' link.
Name your view 'upcomingevents' or something like that and select a view of nodes (not users or the other kinds of views), then click Next.
Add fields: Click the + symbol next to fields and add a Node->title field and a Date field that displays the content from date field.
Add filters: Filter by node->type is event and the from date is greater than or equal to "now" (enter "now as the default value).
Add sorting: Sort by the from date.
Add table display: Change the display from Unformatted to Table.
Limit # of events: Change from 10 to 5 or whatever.
Add block display: In the popup, add a block display.
Then click the Save button to save your view.
Now go to Site Building->Blocks and add the upcomingevents block to any region you want. You probably would only want to show it on the <front> page, too.
Don't Worry
This was kind of a complex topic to cover this week, but it isn't required for your final project. I just wanted to expose you to drupal's views and cck since it is a really powerful feature that lets you build a lot of custom functionality into a website that can be useful for an organization, such as an event calendar, a job board, a list of classes or products offered, etc.
Google for 'drupal views tutorial' or 'drupal views video' to see more info on this, and/or grab a drupal book from Amazon or another bookstore.