timeago.module in Timeago 6.2
Same filename and directory in other branches
Adds support for the Timeago jQuery library.
File
timeago.moduleView source
<?php
/**
* @file
* Adds support for the Timeago jQuery library.
*/
/**
* Converts a timestamp into a Timeago date.
*
* @param $timestamp
* A UNIX timestamp.
* @param $date
* (Optional) A human-readable date (will be displayed if JS is disabled).
* If not provided, the site default date format is used.
* @return
* HTML representing a Timeago-friendly date.
*/
function timeago_format_date($timestamp, $date = NULL) {
timeago_add_js();
if (!isset($date)) {
$date = format_date($timestamp);
}
$elem = variable_get('timeago_elem', 'span');
// D6 doesn't support format_date($timestamp, 'custom', 'c') (see
// https://drupal.org/node/494434) so we use gmdate() instead. Note that
// PHP4 doesn't support date('c') either, so we have to manually construct
// the ISO-8601 format. However, PHP4 doesn't have any way to get the
// ISO-8601 year (date('o') in PHP5) so we have to settle for date('Y') which
// returns the "standard" year. date('Y') is sometimes different than
// date('o') on the last week of each standard year.
if (version_compare(phpversion(), '5.0.0', '<')) {
$time = gmdate('Y-m-d\\TH:i:s+00:00', $timestamp);
}
else {
$time = gmdate('c', $timestamp);
}
if ($elem == 'time') {
return '<time class="timeago" datetime="' . $time . '">' . $date . '</time>';
}
else {
return '<' . $elem . ' class="timeago" title="' . $time . '">' . $date . '</' . $elem . '>';
}
return $date;
}
/**
* Implementation of hook_menu().
*/
function timeago_menu() {
$items = array();
$items['admin/settings/timeago'] = array(
'title' => 'Timeago',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'timeago_admin',
),
'access arguments' => array(
'administer site configuration',
),
'description' => 'Allows administrators to adjust settings for timeago.',
);
return $items;
}
/**
* The administrative settings form.
*/
function timeago_admin($form_state) {
$form = array();
$form['timeago_node'] = array(
'#type' => 'checkbox',
'#title' => t('Use timeago for node creation dates'),
'#default_value' => variable_get('timeago_node', 1),
);
$form['timeago_comment'] = array(
'#type' => 'checkbox',
'#title' => t('Use timeago for comment creation/changed dates'),
'#default_value' => variable_get('timeago_comment', 1),
);
$form['timeago_elem'] = array(
'#type' => 'radios',
'#title' => t('Time element'),
'#default_value' => variable_get('timeago_elem', 'span'),
'#options' => array(
'span' => t('span'),
'abbr' => t('abbr'),
'time' => t('time (HTML5 only)'),
),
);
return system_settings_form($form);
}
/**
* Implementation of hook_preprocess_node().
*/
function timeago_preprocess_node(&$variables) {
if (variable_get('timeago_node', 1)) {
$node = $variables['node'];
$variables['date'] = timeago_format_date($node->created, $variables['date']);
if (theme_get_setting('toggle_node_info_' . $node->type)) {
// We could override theme('node_submitted'), but our override would get overridden by themes.
$variables['submitted'] = t('Submitted by !username !datetime', array(
'!username' => $variables['name'],
'!datetime' => $variables['date'],
));
}
}
}
/**
* Implementation of hook_preprocess_comment().
*/
function timeago_preprocess_comment(&$variables) {
if (variable_get('timeago_comment', 1)) {
$comment = $variables['comment'];
$variables['date'] = timeago_format_date($comment->timestamp, $variables['date']);
// We could override theme('comment_submitted'), but our override would get overridden by themes.
$variables['submitted'] = t('Submitted by !username !datetime', array(
'!username' => $variables['author'],
'!datetime' => $variables['date'],
));
}
}
/**
* Implementation of hook_token_list().
*/
function timeago_token_list($type = 'all') {
$tokens = array();
if ($type == 'node') {
$tokens['node']['timeago'] = t('The amount of time ago the node was created.') . ' ' . t('Uses the Timeago module to display the time dynamically with graceful degredation for non-JS users.');
}
elseif ($type == 'comment') {
$tokens['comment']['created-timeago'] = t('The amount of time ago the comment was changed.') . ' ' . t('Uses the Timeago module to display the time dynamically with graceful degredation for non-JS users.');
}
return $tokens;
}
/**
* Implementation of hook_token_values().
*/
function timeago_token_values($type, $object = NULL, $options = array()) {
$values = array();
$o = (object) $object;
if ($type == 'node' && !empty($o->created)) {
$values['timeago'] = timeago_format_date($o->created);
}
elseif ($type == 'comment' && !empty($o->timestamp)) {
$values['created-timeago'] = timeago_format_date($o->timestamp);
}
return $values;
}
/**
* Overrides the default translation of Timeago dates if necessary.
*/
function timeago_add_js() {
// Add the Timeago library, the module's helper JS, and the default Drupal
// translation of Timeago date terms.
$path = drupal_get_path('module', 'timeago');
drupal_add_js($path . '/jquery.timeago.js');
drupal_add_js($path . '/timeago.js');
// Some languages (Arabic, Polish, Russian, Ukranian, etc.) have different
// suffixes depending on the numbers used in the dates, so we may need to
// have more complex translations than Drupal allows. To support these cases,
// we allow adding a script that will override the translations. Examples
// are available at https://gist.github.com/6251.
$path .= '/jquery.timeago.' . $GLOBALS['language']->language . '.js';
if (file_exists($path)) {
drupal_add_js($path);
}
}
Functions
Name | Description |
---|---|
timeago_add_js | Overrides the default translation of Timeago dates if necessary. |
timeago_admin | The administrative settings form. |
timeago_format_date | Converts a timestamp into a Timeago date. |
timeago_menu | Implementation of hook_menu(). |
timeago_preprocess_comment | Implementation of hook_preprocess_comment(). |
timeago_preprocess_node | Implementation of hook_preprocess_node(). |
timeago_token_list | Implementation of hook_token_list(). |
timeago_token_values | Implementation of hook_token_values(). |