Drupal recipe: dynamic menu link to "most recent" node, plus Disqus

I'm working on a Drupal website implementation for a fellow consultant's portfolio site, including a blog.  As an IA tactic, we want the primary menu item "Blog" to link to the most recent blog entry.

I spent some time researching dynamic paths in Drupal and initially couldn't find anything suitable.  For SEO reasons, I would prefer to be able to click the link in the menu item and automagically go to the proper pathauto'ed blog entry as opposed to having an extra alias applied to the post.  I really didn't want to get into custom code with hook_menu or anything similar if I could avoid it because I don't have any experience with hook_menu stuff and as this is a budget-sensitive client, I don't have the time didn't think I had the time to research doing that correctly.  :-)

So my first solution involved a custom View, displaying the most recent blog item:

  • Filter: Node Published, Blog entry; 
  • Sort: Node Post date, descending
  • Items to display: 1
  • Row style: Node

 I output this as a Page display, gave it a Primary Links menu item, and gave it a URL of /blog/latest -- contrary to my SEO-friendly requirement mentioned above :-(

I then found that the Disqus comment moderation module we were using would only display on a per node type basis... so it wasn't displaying in my view page display.  Total bummer.  Then I stumbled across this helpful post by Drupal.org user narayanis with the snippet below:

<?php print $node; ?>

<?php if(user_access('view disqus comments') && variable_get('disqus_location', 'content_area') == 'content_area'){ $node = node_load($view->result[0]->nid); print theme('disqus_comments', $node->disqus); } ?>

 I saved that as /<themefolder>/templates/views-view-row-node--blog-custom--page-1.tpl.php per the ThemeDev suggestion and voila!  Latest blog post in full, fully formatted, and including the Disqus widget below.  This is the first time I've overridden Views theme templates -- all my previous Views theming has been strictly at the CSS level.  But everything seems to work :)  

When I first tried this, I got a "Object of class stdClass could not be converted to string" error centering on the "print $node;" line.  I was trying to use "node-view-VIEWNAME-DISPLAYNAME.tpl.php" as the template suggestion which doesn't work.  I think this is because $node is an object and not a string.

Instead, I placed the same snippet within "views-view-row-node--VIEWNAME--DISPLAYNAME.tpl.php" and it worked fine. If anyone is feeling "mentorful" and wants to explain why it worked in the second template suggestion file and not the first, I'd be appreciative :-)

Even though it works, this is sub-optimal.  I now have identical content on my site aliased by two different URLs -- whatever the pathauto-generated blog URL would be as well as the path applied in the View of "/views/latest".  I'm no SEO expert but I know that this is usually regarded as bad (Wikipedia's page about URL canonicalization).  Even if Google can do best-effort canonicalization for me within its index, it would be much better for analytics and other purposes to just have one URL for this post.

I decided the best thing to do would be to clock out and read up on menu callbacks in Drupal.  :-)