Skip to content

Instantly share code, notes, and snippets.

@Unifex
Created August 17, 2016 00:55
Show Gist options
  • Save Unifex/4dc75207bbe5a1e6bd6c8870762f96ff to your computer and use it in GitHub Desktop.
Save Unifex/4dc75207bbe5a1e6bd6c8870762f96ff to your computer and use it in GitHub Desktop.
Drupal 7: Find when a node is first published. This uses workbench moderation.
<?php
/**
* Implements hook_preprocess_node().
*/
function my_module_preprocess_node(&$variables) {
$node = $variables['node'];
// First published.
// This query uses workbench moderation to determine the first moderation
// transition that resulted in a published node and takes the timestamp
// from that record.
$result = db_query('
SELECT m.stamp
FROM {node_revision} r
LEFT JOIN {node} n ON n.vid = r.vid
INNER JOIN {workbench_moderation_node_history} m ON m.vid = r.vid
WHERE r.nid = :nid
AND m.state = :published_state
ORDER BY m.stamp ASC
LIMIT 1', array(
':nid' => $node->nid,
':published_state' => workbench_mod eration_state_published(),
))->fetchObject();
if (empty($result->stamp)) {
$date = "Unpublished";
}
else {
$date = format_date($result->stamp, 'long');
}
$variables['first_published'] = t('First published on !datetime', array('!datetime' => $date));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment