Formatting node titles in drupal

Bit disappointed in Drupal. Of course it's a great tool (and it's GPL), but they probably won't be 'there' till version 8 at best with regards to full fledged & intuitive customization.
Not that I really mind modifying the PHP code anyway, but I'd rather create actual content instead of doing that.

So, of course you have installed the cck module to create fields, and it's oh so convenient (although customizing the field formatting can be a bit of a pain there too), but unfortunately, the title field for a node does not fall under the cck realm, and thus you don't have means of applying any of the default or custom formatters you might be using with cck. Bummer!

Let's say you have created a new content type, with type 'my_ct' then (would be listed along with 'book', 'page', 'story'), and you are using a numeric title field (again, with no possibility of setting a content type for node titles and check input, Drupal is quite limited). Now you want to display your title in a custom format. Eg. if your node title value is '12', you want your displayed title to be "My_Content_000012", with leading zeroes.
For that, assuming you are using the default garland theme of Drupal v.6, edit the file drupal/themes/garland/template.php and in "function phptemplate_preprocess_page(&$vars)" add the following:
  // Custom format for my_ct node titles
if ($vars['node']->type == 'my_ct') {
$value = $vars['node']->title;
// Don't apply formatting unless numeric
if (!is_numeric($value))
$vars['title'] = $value;
$vars['title'] = sprintf("My_Content_%06d", $value);
Now, if you want better customization or input validation for titles, you can probably get started on that with the following article.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.