Adding "news and events" or "announcements" function
The site needs the ability to maintain News and Events on the home page, with detail to expand any item. I created a temporary version of this using a forum, but the look of the forum for the expanded content was not acceptable. I reviewed the functionality of the module developed by IBM Internet Technology Group as part of their series,
Using open source software to design, develop, and deploy a collaborative Web site
This series describes a web site they developed based on LAMP and Drupal. They developed a Drupal module for announcements. It has several notable features:
- Items can be created with from-to display dates. Announcements will be displayed starting on the "from" date, and will be automatically removed on the "to" date.
- Items will appear on a listing of all news for the next 4 (or so) months.
- Items can appear on a calendar as events.
- There is a separate abstract for the announcement rather than the Drupal standard trimmed text.
Their module was developed using Drupal 4.7 while the current version is 5.1. I investigated the possibilities and determined that the functionality could probably be built using standard Drupal modules rather than having to write, or rewrite, the IBM module. There is some PHP programming needed, but there is help at least for most of it. Besides, there could be a licensing issue with the IBM code.
The modules that I needed are:
- CCK (Content Construction Kit) to build the custom content type
- Date to provide the from and to dates in CCK
- Views and Views UI to provide a "view" of the content based on the from and to dates
- Content Template to provide a custom presentation of the content fields on the front page and the full page view of the node
- Calendar to provide a calendar showing the dates of the events
This procedure generally follows the process described in Step by Step Setup of Calendar View which provides an excellent tutorial of how to integrate some modules in some needed functionality. I performed the procedure after going through the above, and I hope that I got everything needed. If it doesn't work for you, try following the above tutorial first, and it may pick up something I missed.
I assume that you have a working Drupal 5.1 system. To begin, download and extract modules to the sites/all/modules directory:
- CCK-5.x-1.5.tar.gz
- contentemplate-5.x-1.1.tar.gz
- date-5.x-1.5.tar.gz
- views-5.x-1.5.tar.gz
- calendar-5.x-1.5.tar.gz
Go to Administer > Site Building > Modules and enable:
- Content
- Date (under CCK)
- Text (under CCK)
- Content templates (under Others)
- Date API (under Others)
- Calendar
- Views
- Views UI
Go to Administer > Content Management > Content Types > Add Content Type. I used the following fields:
- Name: News (I decided to use News rather than Announcements because it is shorter ;-) and the home page I am designing for has a section titled "News & Events" which is my target for deployment.
- Type: news
- Description: News and Events
- Under Workflow, deselect Promoted to front page
Click the button Save Content Type.
You should be back at Content Types. On the line for News, click Edit. Click the tab Add Field. In Create New Field, create a field:
- Name: abstract
- Field Type: Text - text field
Click Create Field. This will take you to the Field Settings window.
In Widget settings, for Help Text, enter "Abstract of this news item to be shown on the home page"
In Data settings, select Required and Filtered Text. Click Save Field Settings.
Add another field. As you did above, enter the values:
- Name: news_date
- Field type: Date - Select List
- Label: News Date
- Help text: Date for this news item
- Date Display: 08/01/2007
- Time zone handling: No time zone conversion
Add another field.
- Name: from_to
- Field type: Date - Select List
- Label: Display news from-to
- Help text: Display this news item during this range of dates
- Required
- To date: Required
- Date Display: 08/01/2007
- Time zone handling: No time zone conversion
Add another field for the calendar date:
- Name: calendar_date
- Field type: Date - Select List
- Label: Calendar date
- Help text: optional field for date of event to be shown on the site calendar
- Not required
- Date display: as you prefer
Modify the default calendar view for your new content type.
Go to Administer > Site Building > Views and you should have a Calendar View. Click Add..
- The information under Basic Information, Page, and Block are all fine.
- Under Fields, delete the Node: Updated Time
- Then under Add Field, select Date:Calendar Date and click Add Field
- Next to Calendar Date field, Label can be blank, Handler needs to be Do Not Group Multiple Values
- Arguments can all be left alone
- Under Filters... Add Filter, select Node: Type and click Add Filter
- Operator = Is one of, Value = News
- Exposed filters can be left alone
- Under Sort Criteria, delete Node: Updated Time, Add Criteria Date: Calendar date. Leave order ascending.
- Click Save
Set up a new role for the users who will manage the news. Go to Administer > User Management > Roles. Add role: news editor. Click Edit Permissions. Under Node Module, select
- create news content
- edit news content
Click Save Permissions.
Create views to display the news content on both the front page and the view listing page. There will be three versions of the views:
- one for the front page showing only those news items with from dates before now, and to dates after now
- a view listing page for all users showing all news items with News Dates within the next 4 months (you could use a different period for display)
- and another only for the people authorized to manage the news nodes, which will include all news and all fields on each news item
Go to Administer > Site Building > Views. Click the tab Add. Enter:
- Name: news_front
- Description: news view for front page
My requirement was for a Block to be displayed on the front page.
Under Block, select Provide Block View. Enter:
- View type: Teaser list
- Title: News and Events
- Nodes per block: must be entered, your choice. I used 10 as I wanted to allow for lots of them
Under Filters- Add Filter:
- Select Date: Display news from-to - Date (field_from_to), click Add Filter. In the filter,
- Operator: less than or equal to
- Value: now
Add another filter.
- Select Date: Display news from-to - To Date (field_from_to), click Add Filter. In the filter,
- Operator: greater than or equal to
- Value: now
Add a third filter
- Node: Type
- Operator: is one of
- Value: news
Under Sort criteria: Add criteria: Date: News Date (field_news_date).
At the bottom of the Views window, select Save.
Add another View for the News and Events page. Click the tab Add. Enter:
- Name: news_page
- Description: news view for a page
Under Page, select Provide Page View. Enter:
- URL: news
- View type: Teaser list ( I wanted short items, with the ability to click on the news item for more detail. You could also use Full Nodes.)
- Title: News and Events
- Under Menu, select Provide Menu
Under Filters, Add Filter
- Node: Type
- Operator: is one of
- Value: News
Add another filter:
- Date: News date
- Operator: greater than or equal to
- Value: now
Add another filter:
- Date: News date
- Operator: less than or equal to
- Value: now
- Option: +10368000 (you must scroll to the right to see this column. Value is the number of seconds in 4 months.)
Add sort criteria:
- Date: News date, ascending
Save this view.
Add a third view for the view manager to see all news nodes.
- Name: news_editor
- Access: News editor
- Description: news view for news editor
- Page > Provide page view
- URL: newsedit
- Title: All news and events
- Menu: Provide menu
- Filter: Node: Type is one of news
- Sort Criteria: Add criteria: Date: News Date (field_news_date).
- Click Save.
It is useful to have an example of three news items. I suggest three because you will need to test three conditions: those with start and end dates before today, those with start date before today and end date after today, and those with start and end dates after today. This will test that items appear when they should, and don't appear before or after you expect them to appear. Go to Administer > Create Content > News and make three entries as described above.
If all is well, you should see a new menu item News and Events, and the only entry in that view should be the before and after item.
Next, create Content Templates for the front page and the view pages.
Now go to Administer > Content Management > Content templates. For News, select Create Template.
Click Teaser. Select Affect Teaser Output. (If you are using FCKeditor or other WYSIWIG editor, you will need to edit the Teaser Template using the "source" version.) The Teaser Template will contain code for Abstract, Display news from-to, and News date. SInce I want the abstract and news date to appear in the news views and the front page, I will delete the code generated for Display news from-to. I also don't want the title Abstract, or News date to appear, so I will delete those <h3> items.
<div class="field field-type-text field-field-abstract">
<div class="field-items"> <?php foreach ((array)$field_abstract as $item) { ?>
<div class="field-item"><?php print $item['view'] ?></div><?php } ?></div></div>
There is one more issue with the posting as generated. If the body text is too short to generate a trimmed text version, no "Read more" tag will be generated. To compensate, I will add the following code to the bottom of the Teaser Template:
<?php if (!$node->readmore) {?><a href="<?php print $base_path ?>/?q=node/<?php print $nid ?>">Read more</a><?php } ?>
Click Body. Select Affect Body Output. I want to show the Abstract, From-to Dates, and News Date only to the News Editor. I want to show the body to all viewers. Replace the Body Template with this code:
<?php if (user_access('edit news content')) { ?>
<?php print $node->content['field_from_to']['#value'] ?><?php print $node->content['field_news_date']['#value'] ?><?php print $node->content['field_abstract']['#value'] ?>
<div class="field field-type-text field-field-abstract"><div class="field-label">Body:</div></div>
<?php } ?>
<div class="field field-type-text field-field-body">
<div class="field-items">
<div class="field-item"><?php print $node->content['body']['#value'] ?></div>
</div>
</div>
If you want to copy fields from the Variables blocks, you will need to disable the FCKeditor while editing, then re-enable the FCKeditor.
If you now select the News and Events menu item, you should see the view of the News items, showing the abstract and event date fields, and a Read More link.
In order to see the All News and Events menu item, you must have the News Editor role. Go to Administer > User Management > Users and add the role to the user you want to test. You should see an additional menu item All News and Views, which will show all news and views items so that you can select and edit any one of them.
We're almost done. I want to put the abstracts of the news and events on the front page. I found some code on the Drupal site that will include a view in a page.tpl.php template file. The code is:
<?php
// from drupal.org/node/48816
$view = views_get_view('news1');
print views_build_view('embed', $view);
?>
I added it in my page-front.tpl.php file just before
<?php print $content; ?>
The only issue I have found with this is that if you delete all content except the news and events view, and nothing is left to be promoted to the front page, then the original getting started text appears, which is not what we want. I plan to put a blank dummy abstract in so I have a way to make this manageable. I could remove the print $content code but if I do, I bet there will be a need to include the $content, so I will take the workaround.
An alternative, which I recommend, is to create a block view and use it by adding the block to the front page (and maybe to other pages as well, depending on your site design).
This may all seem like a lot of work, and it is, but I believe it is preferable to building a module if you are only doint it once. Of course, the advantage of a module is that it can be more easily used multiple times and shared with others, so I might do it if I have the time. I have had to implement almost the same thing twice now, so a module is looking better. But the "almost the same thing" causes me to think that the module idea may reduce flexibility that I have with the CKK approach.
- Bill's blog
- Login or register to post comments