-
Dylan Tack
OpenSourcery Alumnus
On the Fuel project, we wanted to customize the date formatting a bit beyond what's available in the Date module. By the time these time strings get to the theme layer — it's too late. Fortunately, CCK formatters are totally pluggable. All we need is two small hooks and a theme function.
When enabled, this module adds a new formatter option to CCK node displays, and to Date fields within Views.

<?php /** * Implementation of hook_field_formatter_info(). */ function datecustom_field_formatter_info() { return array( 'now_playing' => array('label' => t('Now Playing'), 'field types' => array('date', 'datestamp', 'datetime'), 'multiple values' => CONTENT_HANDLE_CORE), ); } /** * Implementation of hook_theme(). */ function datecustom_theme() { return array( 'datecustom_formatter_now_playing' => array( 'arguments' => array('element' => NULL), 'function' => 'theme_datecustom_now_playing'), ); } /** * Theme function to display showtimes. */ function theme_datecustom_now_playing($element) { $start_date = strtotime($element['#item']['value']); if ($start_date <= time()) { return "Now Playing"; } else { return 'Opening ' . date('F j', $start_date); } } ?>
name = Date Custom description = Custom date formatter package = "Date/Time" core = 6.x dependencies[] = date
Nice date tricks
This is a very good enhancement to Dates. With this tip we can now do something similar to Events.
Thank you for sharing this bit of information.
Thanks!
Thanks! This is really useful!
Nice one!
Thanks very much... very useful post!
I want to be able to completely theme the cck date output by inserting some css classes into different elements of the date... would creating a custom formatter like this be the right way to do it?
sounds like a good approach
Yes, I think that would work well, if I'm understanding your question. You want output something like:
?
Very nice...
Thanks for this. I see lots of great applications for this.
I, too, am interested in changing the color of the date elements. Would you mind elaborating a bit more on how this is done, using your datecustom module. That is, how and where would the classes be indicated in the module code?
Thanks again for this very useful contribution!
One other thing...
Me again. One more clarification. I'd like to change the color of the date to, say, green when it displays "Now Playing" and blue when it displays "Opening..."
Thanks again.
D5
I needed something similar to handle embed tags in a textfield as all the formatters that come with cck do some form of processing and cause small problems, but I am using D5, thought I would provide the code if anybody was wondering how to do the same in D5.
embedtag.module
<?php /** * Implementation of hook_field_formatter_info(). */ function embedtag_field_formatter_info() { return array( 'embedded' => array( 'label' => t('Embed Tag'), 'field types' => array('text'), ), ); } /** * Implementation of hook_theme(). */ function embedtag_field_formatter($field, $item, $formatter, $node) { if (!isset($item['value'])) { return ''; } if ($formatter == 'embedded') $text = $item['value']; return $text; }embedtag.info
correction & note
The second function comment should be "Implementation of hook_field_formatter" not "Implementation of hook_theme".
Also, I am using this on a protected internal content type only, most of the text filters are there for a good reason, you probably would not want to dump plain text like this when users are involved.
Changing color
Hans, To change the color like you described, first look at php.net/date to see how to format the dates in different ways. Then add a <span> with a unique class describing the date or date component. Possible classes could be "month", "day", "now", "future". Then style them with CSS.
The CSS could be added to your theme, or the module. To add it to the module look at drupal_add_css().
Handbook
Would be nice if you post your article to the drupal handbook. Maybe her: http://drupal.org/node/262062. Thanks.
Spans around day month year posted as submodule to CCK Formatter
Which addresses a request in a comment here:
http://drupal.org/node/737392
Add your comment