The previous part of the article focused on just a few modules, with an emphasis on date. We explored Date formats, Exposed filters fieldset and the Views Date Range Filter Module. We also had a chance to create some summary views. In this part by Marjorie Roswell, we shall create a Timeline, cover the Views Popup feature, create a Calendar and also make an iCal feed.
This is the first part of a two-part article series by Marjorie Roswell. All of the recipes in this article require CCK and the date field (also Views, and Views UI), so we won't take the space to list them in every ingredient list. In this two-part article, we create a wide variety of views, mostly from a single workshop content type. Several of the views implement style plug-ins, which appear as new options in the Views Type drop-down. If you are still working on the 1.x versions of Date, be sure to check out Recipe 44, Upgrade Date and Calendar Modules which will be discussed in the next part.
Recipe 33: Understanding Date formats
Drupal dates are typically stored in one of two ways. Core Drupal dates—including Node: Created Time, and Node: Updated Time—are stored as Unix timestamps. Contributed module date fields can be stored as either a timestamp or a format known as ISO. Neither style is particularly friendly to human readers, so both field types are usually formatted before users see them. This recipe offers a tour of places in Drupal where dates can be formatted and information on how to customize the formats.
What's that Lucky Day? The Unix timestamp 1234567890 fell on Friday the 13th, in February, 2009. This timestamp marks 1,234,567,890 seconds since January 1, 1970. The same date/time combination would be stored in a date field in ISO format as 2009-02-13T23:31:30+00:0. ISO is an abbreviation for the International Organization for Standardization
Opening the browser windows side-by-side will help you understand date formatting. In the left window, open YOURSITE.com/admin/settings/ date-time to view the settings page for date and time. In the right window, open the API page of code that defines these system date time settings at http://api.drupal.org/api/function/system_date_time_settings/5. Compare each item in the $datemedium array, for instance, with the associated Medium date format drop-down.
Below is the list of codes for many commonly used date and time formats. A more comprehensive list appears at http://us.php.net/date.
a – am/pm
D – Day, Mon through Sun
d – Date, 01 to 31 (with leading zeroes)
F – Month, January through December (mnemonic, F = Full name)
g – Hours, 1 through 12
H – Hours, 00 through 23
i – Minutes, 00 to 59
j – Date, 1 to 31 (No leading zeroes)
l – Sunday through Saturday
m – Month, 01 through 12
M – Month, Jan through Dec
s – Seconds, 00 through 59 (with leading zeroes)
S – Month Suffix, st, nd, rd, or th. Works well with j
Y – Year, Examples: 1999 or 2011
Explore Drupal places where these codes may be used. The first four locations in the table below are available in the Drupal administrative interface. The last three involve editing files on the server—these edits are completely optional.
CCK Date fields are set via the Options drop-down in the Fields fieldset.
Custom date formats for core fields, such
as Node: Created Time are set via
handler and options from elements.
Default Date and Time settings
admin/settings/date-time
Set the default time zone, Short, Medium,
and Long date formats, and the first day of the week.
Post Settings
This may be one of the harder-to-find
settings in Drupal, enabling the Post
settings to be turned-off for specified content types. (An example of a post
setting would be: Submitted
by admin
on Sun, 10/12/2008 - 4:55pm.
The
setting is found on the right-hand side of this URL:admin/build/themes/settings
Use the following mouse click trail to
get to this URL:
Administer | SiteBuilding
| Themes | Configure
(Click on the Configure tab at the top of the
page. If you click on the Configure
link in the Operations column, you will still need
to click the Configure tab at the top to get to
the global settings.)
Variable overrides
in settings.php
You may override
variables at the bottom of the /sites/default/settings.php
file. Remove the appropriate pound signs to enable the $conf array, and add a setting as shown
below. Note that this is a quick way to modify the post settings format,
which draws from the medium date variable.
Note that when using the PHP date codes, additional characters may be added, including commas, spaces, and letters. In the template.php example, a backslash was used to show that the letter 'T' will be printed, rather than the formatted return values. Below are more examples of added characters:
F j, Y, g:i a // August 27, 2010, 5:16 pm m.d.y // 08.27.10
You may occasionally find that an online date converter comes in handy.
This recipe is the foundation for many other recipes in this article.
The Node Go To module is a little-known treasure that makes it easy to enter content. This recipe also makes use of the now option, available in the Date module.
Install and enable all of the modules in the ingredients
Go to admin/settings/date-time and set the default time zone.
Go to admin/content/types/add and create a content type named Workshop, of type workshop. As usual, remove the Body field in the Submission form fieldset. We'll create a workshop_descriptionfield instead
Go to admin/content/types/workshop/add_field and add a field named workshop_date. We have several field types to choose from, however, I recommend the Datestamp format for greater flexibility. Drupal's format_date() function is used to create graphical date badges. This function requires a Unix timestamp as the first parameter (the Drupal Date module uses the term Datestamp for fields stored in Unix timestamp format). The Text Field with jquery pop-up calendar offers a friendly way of entering dates.
Press the Create field button. All the settings at admin/content/types/workshop/fields/field_workshop_date may be left as default, or edited, as per your preference. Click the Save field settings button.
Go to admin/content/types/workshop/add_field, and add a text field named workshop_description. Click the Create field button.
Set the number of rows to 4 and set the Text processing to Filtered text(user selects input format) at admin/content/types/workshop/fields/field_workshop_description. Click the Save field settings button.
The Node Go To module redirects the browser to a specified URL after creating, updating, or deleting a node. Configure the module at admin/settings/nodegoto. Open the fieldset labeled Set the path to redirect for, when INSERTING. Enter node/add/workshop as the path as shown:
Create several workshops, at node/add/workshop. As a result of the configured Node Go To module, after each node is submitted, the entry form for another workshop appears.
Create a new view, at admin/build/views.
Field
Value
Name
workshops
Description
Page and
block of workshops
Provide
Page View
Checked
URL
workshops
View
Type
Full
Nodes
Title
Upcoming
Workshops
Use Pager
Checked
Nodes
per Page
10
Provide
Block
Checked
View
Type
List
View
Title
Upcoming
Workshops
Nodes
Per Block
6
[More]
Link?
Checked
Fields
(use default handlers and options.)
Node:
Title
Datestamp:
workshop_date (field_workshop_date)
Filters
Node:
Published - Equals
Yes
Node:Type
- Is One Of Workshop
Datestamp:
workshop_date - Date (field_workshop_date)
Set the Operator to greater than or equal to.
Enter the word now in the Option field.
Thus we are filtering to workshops that
will be held in the future.
Sort
workshop_date (field_workshop_date) -
Ascending
Save the view
Go to admin/build/block and add the workshops block to the right sidebar. Enjoy the view.
We will add one more set of steps in preparation for Recipes 35 – 41. Clone this view six times at admin/build/views/workshops/clone. Rename each view as follows:
Name
Description
URL
exposed_filters
Exposed
Filters
exposed-filters
summary
Summary
summary
browse_workshops
Browse
Workshops
browse-workshops
timeline
Timeline
timeline
views_popup
Views
Popup
views-popup
For each view you may also uncheck the Provide Block View checkbox and, Save the view.
The GCal Events module http://drupal.org/project/gcal_events offers a handy way of creating blocks of upcoming events. Events are drawn from Google Calendar.
If you are still using Date 1.x, be sure to install the JavaScript Tools module and enable the JSTools Calendar module. This will give you a pop-up calendar selection tool similar to the one available in Date 2.x. You may also implement Recipe 44 to upgrade the Date module.
We finally turn to the Exposed Filters fieldset, which exposes one or more selection lists to users, enabling them to choose how to filter the view (until now, all of our filters have been applied by the web site administrator, rather than by end users). In this recipe, we enable users to filter by the date field. We will explore the Views Date Range Filter module which offers additional options, in the recipe that follows.
Edit the exposed_filters view, at admin/build/views/exposed_filters/edit.
Go to the Filters fieldset and click on the Expose button for the Datestamp: workshop_date field.
The delete icon disappears from the Ops column for that row. However, the field now appears in the Exposed Filters fieldset. If you want to delete a field that has been exposed, you must first delete it from the Exposed Filters, before it is available to delete in the Filters fieldset.
Open the Exposed Filters fieldset. Review how it looks, but leave the default settings intact. (Note that there is no Add Filter drop-down available. Exposed Filters field are added and exposed via the Filters fieldset, as in step 2, above.)
Save your view and go to http://YOURSITE.com/exposed-filters. A new selection list is available.
Recipe notes
Exposed Filters offer powerful search capabilities to end users.
Explore the date_views_filter_handler in date_views.inc to see the code behind date filters. The code is complex, as is typical of date functions, but well-commented, especially in the Date 5.x-2.x version.
Install and enable the Views Date Range Filter module.
Edit the exposed_filters view at admin/build/views/exposed_filters/edit. Go to the Exposed Filters fieldset and click on the delete icon to remove the Datestamp filter.
Click on the Save and edit button (always a good idea after working with the Exposed Filters fieldset).
Go to the Filters fieldset and delete the Datestamp filter there too.
Add the Date Range Filter and press the Expose button.
Save the view and test the various date range options. Compare with the date filter options from the previous recipe.
The selection
list for the Views
Date Range Filter
Compare with the
drop-down list
from Recipe
35
In addition to all the preset ranges (within the past week and in the next month), the Date Range module lets you choose events occurring between two dates (Custom date range) It even allows you to exclude a range of dates (Outside date range) and to specify dates such as today, yesterday, today-7, today-2M, or today-1Y.
Recipe notes
There are some other subtle differences to be found when filtering by date versus filtering by a Date Range. The Views Date Range Filter module allow placement of values such as today+1 or today-1Y in the Value text box of the Filters fieldset. By contrast, Date 2.x allows values such as now and now +1 day. Spacing and capitalization are important. Copy and paste from the helpful notes under the field, or from the README.txt file to ensure the appropriate format.
Recipe 37: Exposed Filter settings
Ingredients Completed Recipes 34 and 35
The Exposed Filters fieldset provides four options, as shown:
The handbook page for filters (http://drupal.org/node/54457) offers helpful documentation on these options. After studying that page, the best way to get to understand these filter options is simply to try them. While testing the options keep an eye on both the Filters and Exposed Filters fieldsets (two of the options: Filter settings Default and Lock Operator correspond directly to settings in the Filters fieldset).
Create a vocabulary for workshop topics at admin/content/taxonomy/add/vocabulary
Vocabulary name
Topic
Description
Topics for workshops
Help Text
Select one or more topics
Types
Workshop
Hierarchy
Disabled
Check
Multiple Select and Required.
Add terms such as Nutrition, For Kids, and Gardening to the Topic vocabulary at admin/content/taxonomy.
Edit the existing workshops nodes and assign appropriate terms. It may be helpful to use the view to identify the workshops. Open the exposed_filters view and instead of choosing Edit (which will edit the view), click on each title link. Now choose Edit to edit that particular node. For the sprouting workshop, assign to it the topics such as Nutrition and Gardening.
Edit the view at exposed_filters/edit. Add the Taxonomy: Terms for Topic taxonomy to the Filters fieldset and click Expose.
Experiment with the Exposed Filters options. Click the checkbox for each of the four Exposed Filters options, save the view, and examine the results. Your results may appear slightly different than the images below depending on whether your taxonomy is set to be required and depending on whether several options are checked at once.
Optional: Unchecked
Does
not include an All
selection, and no workshops are displayed, by default.
Optional Checked
Includes
an All
selection, and all workshops are
displayed.
(The
None
option only appears if the taxonomy is
not required.)
Filter settings Default: Unchecked
All
of the workshops are displayed.
Filter settings Default Checked
Only
the videos are shown, by default, at the exposed_filters
URL. The Video
selection was set in the Filters
fieldset.
Force Single: Unchecked
Force Single: Checked
Lock Operator: Unchecked
Lock Operator: Checked
The
Is
One Of operator from the Filters fieldset is locked in for use by Exposed Filters.
Thus, the
option does not appear for end users.
Recipe notes
If you lock an operator, consider using the Field's Label to give some information on how to use the drop-down values.
Views offers a wonderful feature, called Summary Views, available in the Argument fieldset. It creates a hyperlinked list of topics with a count of items.
Edit the summary view, created in Recipe 34.
Add the Datestamp: Workshop Date argument, with the following settings:
Default
Summary,
sorted ascending
Title
Workshops
in %1
Option
summarize
by month
Wildcard
all
Enjoy the View.
Click on a month link. Notice that the month is included as an argument in the URL. The title of the page substitutes the month for the %1.
Try out the wildcard at http://YOURSITE.com/summary/all. All the workshops are displayed, using the pager count of 10.
We would like the view to display up to 10 workshops per page but prefer to display the next 12 months in the Summary View—so we will add argument handling code to the view. Edit the view at summary/edit. In the Arguments fieldset open the Argument Handling Code and add the following:
code 1::
if (!$args[0]) { $limit = 12; }
Thus, if the argument in the workshop_summary view is not set (for instance, at the URL http://YOURSITE.com/summary, as opposed to http://YOURSITE.com/summary/2010-02 or http://YOURSITE.com/summary/all), then the page limit will be 12 rather than the usual limit of 10 items that were set in the Page fieldset.
You will now see all 12 months in the summary—if you actually have 12 months of upcoming workshops.
(Steps 7–9 are optional.) If you would rather have the view summary display workshops by topic, edit the view at summary/edit.
Delete the argument and the argument handling code. Replace it with Taxonomy: Term Name
Default
Summary, sorted ascending
Title
Workshops - %1
Option
(leave blank, or set to 0)
Wildcard
all
Save the view and open it in your browser.
Recipe notes
The Drupal handbook contains a nice view for creating a monthly archive of content. Even nicer, it is something that can be easily imported into your site. Go to http://drupal.org/node/52037, copy the Import this view code block and paste it into admin/build/views/import. Save the view and place the archive block into a region atadmin/build/blocks. The archive view is similar to the one in Step 2 above, but it is based on the node created time rather than a CCK date field. It also sorts in descending order (with the most recent month at the top) and includes all site content, by default.
The Views wildcard is commonly set to all. Of course, if Nodes per Page is set to 10, it really only shows 10 nodes at a time and not all of them. It may be preferable to set the wildcard to the word browse, making the URL: YOURSITE.com/<VIEW-NAME>/browse.
Recipe 39: Date Browser
Ingredients Completed Recipe 34
Views, by default, come with four View types: Full Nodes,Teaser List,Table View,and List View. The View Types selection list is populated by Views style plugins coded in modules. These style plugins add options to the View Types drop-down in both the Page and Block fieldsets. Style plugins can provide dramatic alterations in node formats. We'll have a chance to look at a date browser, a timeline, a views popup, and a calendar, in Recipes 39 - 42.
Edit the browse_workshops view created in Recipe 34. As usual, you may visit either browse_workshops/edit (this URL uses the general theme) or admin/build/views/browse_workshops/edit (this URL uses the administrative theme).
Set the Page View Type to Date: Date Browser and Save the view. A message is displayed:
A date argument must be added to a Date Browser view.
Add the Datestamp: field_workshop_date field to the Arguments fieldset and Save the view. Another message is displayed:
Date Browser arguments must be set to 'Display All Values'.
The Date module is kindly guiding us through the appropriate settings for the Date Browser view type.
Go back to the Arguments fieldset and set the Default to Display All Values. While there, also set the Option to summarize by month.
Save the view and browse to the browse_workshops page. The first page displays the current month, with a pager. Additional links are displayed for next month and previous month.
Let's go ahead and add a block to this view. Go to admin/build/views/browse_workshops/edit. Check the Provide Block box and set the block View Type to Date: Date Browser.
Add this block to a sidebar in admin/build/block.
The date browser module creates a small block with a single link for the current period (the period—Year, Month, Day is determined by the view argument option).
Recipe notes
The Date module offers a number of theme functions for modifying output. Date 5.x-2.x helpfully has twice as many theme functions as Date 5.x-1.x. Regardless of which Date module version you are using, one good way to determine the function names and their associated filenames, is via the grep command. Log on to your host. Go to sites/all/modules/date, and enter the following command:
grep -r theme_ *
Summary
This article focused on just a few modules, with an emphasis on date. We explored date formats noting a broad range of Drupal locations where the special formatting for PHP dates can be applied. We explored the Exposed filters fieldset including some special filters enabled by the Views Date Range Filter Module. We had a chance to create some summary views. In the next part, we will cover the Timeline and Calendar modules.
Drupal 5 Views Recipes
94 recipes to develop custom content displays for your Drupal web site
Display particular types of content in unique and compelling ways on your Drupal web site
Enhance your web site with calendars, timelines, galleries, maps, podcasts, Views Fusion, and more
Indispensable resources for Drupal 5 Administrators – Drupal Administration Menu, Views Bulk Operations, ModuleInfo, and Editable Fields modules
More than 90 recipes – pick the ones that work best for your web site
Marjorie Roswell is a web developer from Baltimore, MD. She has been building web sites that serve the community for more than a decade.
Marjorie developed a GIS system for assisting citizen callers to the Baltimore Office of Recycling, and has taught professional classes in desktop publishing, AutoCAD, and Drupal.
While in college, Marjorie received the Betty Flanders Thomson Prize for Excellence in Botany. She now brings together her love of plants and Drupal to create BaltimoreUrbanAg.org, the web site for the Baltimore Urban Agriculture Task Force.
Marjorie enjoys writing and playing music with a community of local musicians. You can hear some of her music at FriendlyCoffeehouse.org.