Adding Content Between Drupal Book Navigation and Content

I recently wanted to add a variable to my book template with a list of contributors to a wiki page. My "wiki" is a node type and I am using the excellent revision moderation module to let users submit revisions to nodes. I really want to recognize those who submit revisions. To do so, I decided to add a list of revision authors on each wiki page.

This ended up being trickier than I expected. First, I ran into difficulty getting my list to show up in the correct spot. If I added my list to the node template (node-wiki.tpl.php), then it would show up below the book navigation. However, I wanted my list to show up below the main node content but above the book navigation.

To get the list to show up in the correct location, I ended up adding the following to book-navigation.tpl.php in my custom Zen subtheme.

  1. if (isset($revision_authors)):
  2. print theme_item_list($revision_authors, $title = 'Contributors', 'ul', array('class' => 'contributors')); ?>
  3. endif;

You might be wondering where $revision_authors is coming from. To get this variable to show up in my book-navigation.tpl.php template, I had to first define it in template.php. To add a variable to book-navigation.tpl.php, you need to create a special template preprocess function. Mine is called mytheme_preprocess_book_navigation((&$variables) and is in my template.php file.

  1. /**
  2.  * Adding $revision_authors to book-navigation.tpl.php
  3.  */
  4. function mytheme_preprocess_book_navigation(&$variables) {
  5. $variables['revision_authors'] = get_all_revision_authors(arg(1));
  6. }

Finally, if you're curious, here's the function I'm using to retrieve the revision authors. I am passing arg(1), which corresponds to the node ID of my wiki nodes. I am only retrieving the authors of approved revisions.

  1. /**
  2.  * Retrieve list of all revision authors for a given node.
  3.  *
  4.  * @param $nid
  5.  * The node ID to retrieve.
  6.  */
  7. function get_all_revision_authors($nid) {
  8. //Get revision authors for a node
  9. $sql = "SELECT DISTINCT r.uid AS uid FROM {node} n INNER JOIN {node_revisions} r ON n.nid = r.nid WHERE r.vid < n.vid AND n.nid = %d ORDER BY r.vid DESC";
  10. // $sql = 'SELECT DISTINCT uid FROM {node_revisions} WHERE nid = %d ORDER BY timestamp DESC';
  11. $result = db_query($sql, $nid);
  12. $revision_authors = array();
  13. while ($revision_uid = db_fetch_object($result)) {
  14. // $revision_authors[] = $revision_uid;
  15. $revision_authors[] = theme('username', user_load(array('uid' => $revision_uid->uid)));
  16. }
  17. return $revision_authors;
  18. }

Post new 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: <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]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.

More information about formatting options