Notes on custom content types in Drupal 6, mainly about the modules used to create custom content types.
Index
Initial setup
Either create a module or use the Content Construction Kit (CCK)
Create new content-type for Drupal 6.x
To get rid of a default field, like the body, edit the type’s submission form settings and set the field label to blank.
Bug alert! There is a bug in CCK / Views whereby deleting a CCK field from a content type will result in error messages in the View UI when the field is being used by a view. You must remove a field from all views before deleting it from a content type.
CCK
Also see: Adding Fields With CCK
- Install module and enable all submodules.
- Create a new content type from Administer -> Content management -> Content types.
- Use ‘Manage fields’ option to define fields and field groups for the new content type.
- With the CCK Content Permissions “sub”-module enabled, for each field you add to the new content type, you need to set the edit and view permission in the ‘content_permissions module’ section of Administer -> User management -> Permissions.
- To remove the body field from a new content type, edit the type and in the ‘Submission form settings’ section blank out the ‘Body field label’ field.
- Title can not be removed, but the label can be changed in the ‘Submission form settings’ section mentioned above.
- The appearance of custom fields in a content type’s displays (e.g. Basic, RSS, Token) can be configured via Administer -> Content management -> Content types, edit type, ‘Display fields’ tab.
CCK fields
There are many modules to add new CCK field types. Here are some of those I use often…
Date
- Install the module and enable Date, Date API, Date Timezone, Date Popup.
ImageField
Requires FileField
- Install as normal.
- Give role permission to ‘view image field’ via Administer -> User mangement -> Permissions.
- Configure settings via Administer -> Site configuration -> File system, File uploads and Image toolkit.
- To have a view display the image, choose ‘Content: Image’ as a view field and configure it to have format ‘Image’ (via Administer -> Site building -> Views).
Note that uploading an image may not work if you have the devel module enabled.
Conditional Fields
- Install as normal.
- Administer -> Site building -> Content types, ‘manage fields’ for a type.
- There should be a new ‘Conditional field settings’ section when you configure a field, which allows you to set which value of another field will trigger the display of the field you’re configuring.
- Show/hide animation settings can be found via ‘Conditional fields’ tab.
Link
For link fields. It can validate the link, use tokens to set its title (e.g. to the title of the node) and doesn’t mind if http:// is entered or not.
Views
- Install modules and enable all submodules.
- Administer -> Site building -> Views -> Add tab to create a new view.
- Add a new ‘Page’ display and select it.
- For each set of attributes, click an attribute to change it (selecting Override to avoid changing defaults), click the cog icon to configure it and click the plus icon to add another attribute.
- Basic settings -> Style to change view display, e.g. Table.
- Add a filter to reduce content set. E.g. Node:Type is one of Page.
- Add fields to display. E.g. Node:Title and Node:Body. Use Content group for CCK fields.
- Other useful option is Use Ajax: Yes.
- Define a path in Path and Menu parts of Page section.
- Save the view.
A useful feature is the abilit to rewrite the markup generated by a field. For example, you can create a linked image block (e.g. for ads) by doing the following with CCK/Views:
- Create a new content type via CCK, giving it an image field (via the ImageUpload views module) and a text field for a URL. Note that ImageUpload will give the image a couple of other fields, e.g. alt text, description and title, depending on how it is configured.
- Create a view, set to a Block display, with Row style ‘Fields’.
- Add the CCK image and URL fields - ‘Content: Image’ and ‘Content: URL’, though the names may differ depending on what you named the fields when you created them.
- Add the other image “sub”-fields that ImageUpload associated with the image, by choosing ‘Content: Image (field_image) - data’ and selecting the appropriate data key, e.g. alt text, description or title.
- Set the format of the image field to ‘URL to file’.
- Exclude all fields from display except for image field.
-
Reorder the fields so that the image field is last.
Content: URL Plain text Content: Image - data description Content: Image - data title Content: Image - data alt Content: Image URL to file
-
Configure the image field and set it to ‘Rewrite the output of this field’, using the following markup to makes use of the replacement patterns we have available (by putting all other non-displaying fields first):
<a href="[field_url_value]" target="_blank" title="[field_image_data_1]"><img src="[field_image_fid]" alt="[field_image_data_2]"/></a>
- Set the block to display as normal. It should show the image as a link to the URL.
Note that I have written a module called ‘Ajax Block’ which can be used to refresh a block via AJAX, hence achieving rotating images/ads/news etc.
The Access method in Basic Settings section only controls access to the view, not the items in the view. The View Own module can restrict the display of items in a view to those created by the current user.
How to sort an ID field
Some fields only store the ID of some other content, preventing them being sorted. For example, if you had a content type with a field that referenced a different content type. To get around this problem:
- Create a relationship for the referenced content type.
- The fields for the referenced content type should now be available.
- Add one of those fields and it should be sortable.
See HOWTO: Views 2 Relationships screencast.
Adding a view to a menu
Views (at least page views) provide config so you can add them to a menu. However, they only allow you to create an item at the top level of a menu and the functionality seems buggy in that the menu item sometimes disappears when you later edit the view.
Better to give the view a page and create a menu item via Administer -> Site building -> Menus.
Views Bonus Pack
Provides a number of extras for the views module - data export, panels for layout, lineage nest taxonomy, audio playlists (last two not complete in Drupal 6).
For CSV export of views…
- Install module as normal and enable the ‘Bonus: Views Export’ “submodule”.
- Administer -> Site building -> Views.
- Create new view and set up as normal.
- Give it a new display of type ‘feed’.
- Change its style to ‘CSV file’.
- Give it a path.
- Save, then view the path and you’ll get a download for the CSV.
SWF Tools
Embed media players for attachements.
- Install module as normal, enabling ‘SWF Tools’.
- Administer -> Site configuration -> Input formats.
- Enable ‘SWF Tools filter’ for whichever input formats you use.
- Create a page and with an mp3 attachment (e.g. mpthreetest.mp3).
- Add the following to the page to include the player: [swf file=”mpthreetest.mp3”]
- There are many more config options available, e.g. third party players for all kinds of media types. See http://drupal.org/node/303203