Last updated December 3, 2009. Created by adaven on November 19, 2008.
Edited by Grondhammar, ronald_istos, bekasu. Log in to edit this page.
Description
This snippet allows you to use custom tokens in your footer message to display, for example, a dynamic "last updated" date.
Step 1 of 2
In your template.php create the function mytheme_preprocess_page, where mytheme is the name of your theme.
<?php
function mytheme_preprocess_page(&$vars, $hook) {
// get date of most most recent change to a node
$last_updated = db_result(db_query('SELECT FROM_UNIXTIME(MAX(changed)) FROM {node}'));
// define replacement tokens for our footer message
$tokens = array(
'!last_updated' => $last_updated;
);
// replace tokens in footer_message
$vars['footer_message'] = strtr($vars['footer_message'], $tokens);
}
?>Step 2 of 2
Then, in admin >> Site information, you can put something like "Last updated: !last_updated" in the footer field, and voila! Now your pages will have a dynamic footer showing the last update to your site.
The next step
If you want to go a bit further, you can define new tokens - of pull in tokens from the token module.
If you want to be able to embed PHP in your footer, then simply put the following code in your mytheme_preprocess_page function. Use with caution though...
<?php
$var['footer_message'] = drupal_eval(variable_get(variable_get('site_footer', FALSE)));
?>
Inserting "last updated" time using Views
You can also achieve this same functionality by simply using a view.
Here are the steps to create the view:
1. Check the box to provide a block (when creating the view). You should also set to show 1 Node per block. Set the view type to list.
2. In the Fields section, add the field Node: Updated Time. You can set the Handler to anything you want.
3. In the Filters section, select Node Type and then highlight what node types you want to check updates for. (It's also a good idea to check that the node is published as well).
4. In the Sort Criteria, add the field: Node: Updated Time and order by Descending.
Then you can insert this block view you just created in any region in your theme.
Comments
Get WSOD
When I try to implement this function to include it in the footer, I get a White Screen of Death. I've put this in my template.php file in /sites/all/themes/name_of_my_theme and changed the function call to name_of_my_theme_preprocess_page. In the footer (under Site Configuration/Site Information) I put "Last updated: !last_updated" (without the "). I have also tried
<?phpecho "Last updated: !last_updated";
?>
Re: WSOD
Yes, there is an error in the code above. ; vs ,
So the right piece of code is:
$tokens = array('!last_updated' => $last_updated,
);
Jan Polzer, Drupal specialist and writer.
HTML
I'm using the Advanced Frontpage Module for a custom home page. How would I utilize this code within that HTML vs a Footer?
last updated value for the page (node, actually)
My users asked to have the date and time of the last update for each page displayed, and I got that with a small tweak
to the code above. I don't do that much with Drupal, but I think this is in line with good coding standards. YMMV.
function YOUR_THEME_HERE_preprocess_page(&$vars, $hook) {
$tnode = $vars['node'];
$nid = $tnode->nid;
// get date of most most recent change to the node used for content
$last_updated = db_result(db_query('SELECT FROM_UNIXTIME(changed) FROM {node} n WHERE n.nid = %d', $nid));
// define replacement tokens for our footer message
$tokens = array('!last_updated' => $last_updated,
);
// replace tokens in footer_message
$vars['footer_message'] = strtr($vars['footer_message'], $tokens);
}
Sub-optimal performance
Note that this can have performance implications on your site if you have too many nodes and your site is kind of a "busy" one.
Another approach with better performance, you will write a module that does the following:
hook_nodeapi()variable_set)--
Amr Mostafa (aka “alienbrain”)