Step by step guide to create an event listing with calendar block in Drupal 7
The contents of this guide should be tested along the Events calendar feature module as a working example. It is recommended to install and test this feature module first and dive into the step by step guide if needed.
If you get "The calendar row plugin is required when using the calendar style, but it is missing." notices, you should uninstall the feature and follow the steps below.
Below are the list of steps to create an events listing with a calendar block which filters them.
Download and enable the required modules
- Latest development build of calendar, date, ctools and views modules
- Context and context_ui.
- Features (needed by the feature module mentioned above).
Create an Events content type
Go to Structure > Content types > Add content type (admin/structure/types/add) and create one with the following settings:
- Name: Event
- Fields: Add a field of type Date, field name
event_dateand label Date(s). In the field configuration screen check the "Required field" and "Collect an end date" checkboxes. - Create three or four events at the "Add content" (
node/add/event).
Create a view to list events
Go to Structure > Views > Add new view (admin/structure/views/add) and create a view showing nodes of type "Event". Click on "Continue and edit".
Set the path of this page view to /events.
Add the following Contextual Filter (find the link at the Contextual Filters block when editing the view - often hidden under Advanced on the right - you need to click it to show more):
- In the search field type "Date: Date (node)" - select (Apply) then on the next menu
- "Add and configure contextual filters".
- go to "When the filter value is NOT in the URL", select "Provide default value".
- At Type in the drop-down list, select Current Date.
- Now scroll down to Date field(s) and select "
Content: Event Date - start date (field_event_date)" / "field_event_date"- this is the date you set when creating the Content Type Event. You may have chosen different names - so be aware, especially if you already have dates used in other Content Types. - Scroll down further to Method, select "
AND". - Save.
Create a calendar block for events
- Clone the view "Calendar" into "Events calendar". ›› UPDATE (2012-08-23, related to Calendar 7.x-3.4): you should rather use "Add view from template" on
admin/structure/viewsdepending on the appropriate field. Sample screenshots: "Add view from template"; Views + Calendar templates. - Select the Block display and add a filter by content type so it only lists content of type "Event".
- Edit the Contextual Filter so it looks exactly the same as the one we configured at the Events view. ›› UPDATE (2012-08-23, related to Calendar 7.x-3.4): this is already set when using the appropriate template (see the screenshot in step 1).
- Save.
Add the calendar block to the events view
- Using Context module, create a context (
admin/structure/context/add) so if the condition is the Events view, display the "Events Calendar" block in the sidebar - Save the context
- Verify that opening
/eventslists the created events with a calendar block at the sidebar.
Link the calendar block links with the events view
Add the following code to your default theme. Remember to change "yourtheme" by the name of the default theme. This code shoud reside at sites/all/themes/yourtheme/template.php
/**
* Alters link url in calendar events block in order to filter events at /events
*
* @see template_preprocess_calendar_datebox()
*/
function yourtheme_preprocess_calendar_datebox(&$vars) {
$date = $vars['date'];
$view = $vars['view'];
$day_path = calendar_granularity_path($view, 'day');
$vars['url'] = 'events/' . $date;
$vars['link'] = !empty($day_path) ? l($vars['day'], $vars['url']) : $vars['day'];
}
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion