Quick custom CCK date formatter

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.

Before and After

datecustom.module

<?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);
  }
}
 
?>

datecustom.info

name = Date Custom
description = Custom date formatter
package = "Date/Time"
core = 6.x
dependencies[] = date

Tagged as: CCK, date, Drupal

10 comments

Jacques (xmacinfo) (not verified) wrote 1 year 2 weeks ago

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.

Bevan (not verified) wrote 1 year 2 weeks ago

Thanks!

Thanks! This is really useful!

Jack (not verified) wrote 1 year 2 weeks ago

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?

Dylan Tack wrote 1 year 2 weeks ago

sounds like a good approach

Yes, I think that would work well, if I'm understanding your question.  You want output something like:

<span class="month">January</span> <span class="day">21</span>

?

Hans (not verified) wrote 1 year 2 weeks ago

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!

Hans (not verified) wrote 1 year 2 weeks ago

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.

tbartels (not verified) wrote 1 year 2 weeks ago

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

name = embedtag
description = CCK formatter for embed code text
dependencies = content
package = "CCK"

tbartels (not verified) wrote 1 year 2 weeks ago

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.

Dylan Tack wrote 1 year 2 weeks ago

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().

Ositblanco (not verified) wrote 1 year 1 week ago

Handbook

Would be nice if you post your article to the drupal handbook. Maybe her: http://drupal.org/node/262062. Thanks.

Add your comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options