I'm looking for a simple piece of code (or a module) that would make the "homepage" link redirect me to the latest node in a given taxonomy list (ie: the latest published comic for a comic strip). I want it to be dynamic (so I can use it in tandom with the scheduler module, and have the site post strips automatically). It should just goto the most recent (based on timestamp, and published status) node in a specific taxonomy list with 2 terms (I have two types of strips, memorial in the event of someone passing, and standard, so it needs to go to the most recent of the two of them).

Help?

I've tried the Views module, and it works great... minus the fact that I have a few node-dependent blocks, and when a node isn't onscreen (and it's just a "view" of the latest post) they won't display content (ie: service links, and fivestar). Hence, I need to be on a specific node page for these to work.

Comments

nevets’s picture

When you say "specific taxonomy list" do you vocabulary?

Shivian Balaris’s picture

Sorry, yes. Two terms from the same vocab.

nevets’s picture

Are they the only terms in the vocabulary or are the a subset?

Shivian Balaris’s picture

only two terms in this given vocabulary

nevets’s picture

Here is a simple module called latest, it has two files (below). Create a folder under modules called 'latest', create the two file latest.module and lastest.info (in the 'latest' folder). Enable the module, optionally set the default vocabulary to use. To use, use the path ''latest' (uses the default vocabulary if one is defined), or 'latest/{vid}' where {vid} is a vocabulary id (number). You can make this your default home page under "Administer" -> "Site configuration" -> "Site Information" by setting 'Default front page:'

latest.info

name = Latest Node
description = Displays the latest node in a given vocabulary

latest.module

<?php

function latest_page($vid = 0) {
	if ( $vid ) {
		$vid = variable_get('latest_vocabulary', 0);
	}
	$sql = "SELECT n.nid FROM {node} n JOIN {term_node} tn USING(nid) JOIN {term_data} td USING(tid) WHERE td.vid = %d";
	$nid = db_result(db_query_range($sql, $vid, 0, 1));
	if ( ! $nid ) {
		drupal_not_found();
		return;
	}
	
	$node = node_load($nid);
	
	return node_page_view($node);
}

function latest_menu($may_cache) {
  $items = array();

  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/latest',
      'title' => t('Latest'),
      'description' => t('Configure latest vocabulary.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('latest_settings'),
      'access' => user_access('administer site configuration'),
      'type' => MENU_NORMAL_ITEM
    );
   $items[] = array(
   	'path' => 'latest',
   	'title' => t('Latest Content'),
    'callback' => 'latest_page',
    'access' => user_access('access content'),
    'type' => MENU_CALLBACK
   );
  }

  return $items;
}

function latest_settings() {

	$options = array(0 => 'Select vocabulary');
	$vocabs = taxonomy_get_vocabularies();
	foreach ( $vocabs as $vocab ) {
		$options[$vocab->vid] = $vocab->name;
	}
	$form['latest_vocabulary'] = array(
		'#type' => 'select',
		'#title' => t('Default Vocabulary'),
		'#options' =>  $options,
		'#default_value' => variable_get('latest_vocabulary', 0),
		'#description' => t('The default vocabulary to use to select the latest content.')
	);
	
  return system_settings_form($form);
}
Shivian Balaris’s picture

This works very well, and I think it's close. Two things:

  1. The item shown is the oldest, not the newest piece of content in a given vocab
  2. It still isn't forwarding to a given piece of content, just displaying it, while the modules still think it's a taxonomy page (hence, modules that are for a given node, that side as blocks in the sidebar, don't function)

Is there any way to fix it so that it actually fully forwards a user to the latest piece of content, not just displaying it as the homepage? While I love the simplicity of this, it's also what the Views module can already do :-(

nevets’s picture

It will change the sort order and do drupal_goto() which should handle point (2).


function latest_page($vid = 0) {
if ( $vid ) {
$vid = variable_get('latest_vocabulary', 0);
}
$sql = "SELECT n.nid FROM {node} n JOIN {term_node} tn USING(nid) JOIN {term_data} td USING(tid) WHERE td.vid = %d ORDER BY n.created DESC";
$nid = db_result(db_query_range($sql, $vid, 0, 1));
if ( ! $nid ) {
drupal_not_found();
return;
}
drupal_goto("node/$nid");
}

Shivian Balaris’s picture

That's it dude, awesome!! :-D

Shivian Balaris’s picture

Can someone tell me how to add in a verifier to check to make sure it's the latest PUBLISHED node? It's trying to take me to an unpublished node :-(

nevets’s picture

In the sql change WHERE td.vid = %d to WHERE td.vid = %d AND n.status = 1

Shivian Balaris’s picture

Now, one last question, is there any way to create a code to put into the following section:

"Show block on specific pages: Show if the following PHP code returns TRUE (PHP-mode, experts only)."

So that the block only appears on the "latest" strip page (ie: the homepage, via the "latest" module)?

nevets’s picture

Are you making that the homepage unde "Administer" -> "Site confiigure" -> "Site information", ?Default front page:"?

Is so you can configure the block to only show on the home page by setting "Show block on specific pages" to "Show on only the listed pages." and under "Pages" adding "" (without the quotes).

Shivian Balaris’s picture

Sadly, since the module redirects to a specific node (the most recent in a taxonomy) it does not work. Hence why I was thinking a simple string of PHP to evaluate whether the user was looking at the latest node in that specific taxonomy list (and if so, display the block) would work instead.

Shivian Balaris’s picture

Tried setting the block to display only on "<front>" or "latest/1" and neither works...

summit’s picture

Interesting.
Greetings,
Martijn

merakli’s picture

I tried this in D6 and got an error:

warning: Missing argument 1 for latest_menu() in latest.module on line 16.

and all possible paths are returning "Page Not Found":
/latest
/latest/{vid}
admin/settings/latest