OUTDATED: An example - Theming flexinode

Since it took me a while to make sense of this I thought I would post an example to help others along the way. It's actually quite simple, just not very intuitive.

This example is for changing the way that the flexinode 'date/time' field will display on a page. (I only wanted month and year to show). But could very easily be adapted to other things.

My theme is called 'licc' - it is a phptemplate theme.

The process goes like this:

1. find the theme function for the flexinode field

Found in modules/flexinode/field_timestamp.inc

<?php
function theme_flexinode_timestamp($field_id, $label, $value, $formatted_value) {
 
$output = theme('form_element', $label, $formatted_value);
 
$output = '<div class="flexinode-timestamp-'. $field_id .'">'. $output .'</div>';
  return
$output;
}
?>

This is just for reference, you could just as easily look in the API documentation. Core documentation is here:
http://drupaldocs.org/api/head/group/themeable
(only core modules seem to be online at the moment, so you will need to search through the code for any add-on modules like flexinode)

2. Create template.php and add override function

For my theme I created themes/licc/template.php and then copied the function declaration from above replacing the word 'theme' with 'phptemplate'.

<?php
function phptemplate_flexinode_timestamp($field_id, $label, $value, $formatted_value) {
}
?>

add in the phptemplate callback:
<?php
return _phptemplate_callback('flexinode_timestamp', array('field_id' => $field_id, 'label' =>
$label, 'value' => $value, 'formatted_value' => $formatted_value));
?>

This function doesn't really _do_ anything except give phptemplate control, the next step looks after the actual formatting. Note how the variables are passed on to the _phptemplate_callback() in an associative array.

3. Create flexinode_timestamp.tpl.php to do formatting

This goes in your theme directory (for me themes/licc/flexinode_timestamp.tpl.php). As you can see the name matches the bit after 'phptemplate_' in the theme override function, and the first argument of the _phptemplate_callback().

Put the HTML/PHP that you want in this file for the display of all date/time (timestamp) fields in all flexinode pages.

Something like this:

<div class="flexinode-timestamp-<?php print $field_id; ?>">
<strong><?php print $label; ?>: </strong><br />
<?php print $formatted_value; ?>
</div>

Example Files

template.php

<?php
/***
* template.php
*
* This file contains functions for over-riding the default theme functions
* in Drupal core and modules (look at the API documentation for more info).
* The functions don't actually _do_ anything, except pass the variables
* available to phptemplate for use in the *.tpl.php files.
*
* Add similar 'stub' functions to override other default theme functions.
*/


/**
* Override theme_flexinode_timestamp() from modules/flexinode/field_timestamp.inc
*/
function phptemplate_flexinode_timestamp($field_id, $label, $value, $formatted_value) {
 
// like I said, nothing happens here.
 
return _phptemplate_callback('flexinode_timestamp', array('field_id' => $field_id, 'label' => $label, 'value' => $value, 'formatted_value' => $formatted_value));
}
?>

flexinode_timestamp.tpl.php

<?php
/***
* Customised formatting of flexinode timestamp data in nodes.
* These fields are available:
* $field_id, $label, $value, $formatted_value
**/

// Change the default $formatted_value so that it suits me (no time or day)
$formatted_value = strftime ("%B %Y", $value); // format as Month and Year, eg. 'July 2004'

?>

<div class="flexinode-timestamp-<?php print $field_id; ?>">
<strong><?php print $label; ?>: </strong><br />
<?php print $formatted_value; ?>
</div>

 
 

Drupal is a registered trademark of Dries Buytaert.