How might I do this?

I have created a content type specifically for our "Letters from the President" of our organization. We would like to have an address, say: domain.org/letter that always displays the newest letter (as a full node with comments visible). This is so that we can publish this address in our printed newsletter.

The method that I tried was to create a view that showed the one most recent letter as a "full node" but the problem is that this method doesn't display the comments (and unfortunately, it doesn't even display a "number of comments" link... just "add comment").

Any ideas about how to accomplish this without having to manually edit the alias to the newest post each time one is published?

Thanks very much for considering it...

PS: One other idea was to use this method: http://drupal.org/node/255456 to display the comments as a view on the node, but I'm not quite following it. I don't see "comments" as an option when adding the viewfield to the content type...

Comments

dccircuit’s picture

Hi... This is kind of a "bump"... Still looking for a suggestion about how to handle this... Any ideas? Thanks.

optalgin’s picture

You can write a simple module to do this for you (I can help if you like)

optalgin’s picture

I made a simple module for you
You can edit the file and change the path, alias, node type and descriptions

What it does:
when you go to www.yoursite.com/letter
The module will search the newest node of a given node-type
set an alias for it (i.e. 'letter-from-the-president')
and redirect the page to this page

* I cannot attach files here so here is the code, if you like I can send you the files by e-mail

last_letter.info

name = "Newest letter"
description = "Redirect to the president's newest letter"
core = "5.x"

last_letter.module

/**
 * Implementation of hook_menu
 */
function last_letter_menu($may_cache) {

  $items = array();

  if ($may_cache) {
    $items[] = array(
      'path' => 'letter',
      'title' => t('Last letter'),
      'description' => t('Latest letter from the President'),
      'callback' => 'last_letter_redirect',
      'access' => user_access('access content'),
    );                                   
  }

  return $items;
}  

function last_letter_redirect() {
    
    $node_type = 'story';
    $alias = 'letter-from-the-president';
    
    // Find latest letter
    $query = "SELECT nid FROM {node} WHERE type='%s' ORDER BY created DESC";
    $nid = db_result(db_query_range($query, $node_type, 0, 1));
    
    // Set alias and redirect
    path_set_alias('node/'.$nid, $alias);
    drupal_goto($alias);
}
merakli’s picture

Can this module be made to accept content type as an argument and then redirect user automatically to the latest published node that is in the given content type?

Say we have a content type called 'letter' and another content type called 'envelope'

if user visits: /content/letter, he is redirected to the latest letter node
and if user visits /content/envelope, then he/she is redirected to the latest envelope node.

How can this be accomplished?

arg(1) from request_uri? All types pre-defined? How? Any ideas?

Thanks.

optalgin’s picture

There are few different ways to do this and each way requires different amount of coding

The generic way requires to add parameter handling, admin page to select the desired content types and a menu to the admin page
While this is not too complicated it requires some work

If you know exactly the content types you wish to use you can simply define hard coded handling for arguments
While this is not a generic solution it is very easy to do

Here is an example of the 2nd (easier way)
If it's not enough let me know and I'll try to help you

* I didn't test it, but is should be something like

latest_content.info

name = "Latest content"
description = "Redirect to the latest node of a given content type"
core = "5.x"

latest_content.module

/**
* Implementation of hook_menu
*/
function latest_content_menu($may_cache) {

  $items = array();

  if ($may_cache) {
    $items[] = array(
      'path' => 'content',
      'title' => t('Latest content'),
      'description' => t('Latest content of a given node type'),
      'callback' => 'latest_content_redirect',
      'access' => user_access('access content'),
    );                                  
  }
  return $items;
} 

function latest_content_redirect($content_type) {
   
    // Restrict the redirection for given content types
    $allowed_types = array('letter', 'envelope');

    $node_type = strtolower($content_type);
    if (in_array($node_type, $allowed_types)) {
        $alias = 'content/'. $node_type;
   
       // Find latest letter
       $query = "SELECT nid FROM {node} WHERE type='%s' ORDER BY created DESC";
       $nid = db_result(db_query_range($query, $node_type, 0, 1));
   
       // Set alias and redirect
       path_set_alias('node/'.$nid, $alias);
       drupal_goto($alias);
    }
    else {
       drupal_not_found();
    }
}
munge83’s picture

Can you make this for drupal 7
thanks

optalgin’s picture

Sorry for late response,
I strongly recommend using views module instead, it is very simple to configure such view
Unless you have a reason not using views this is the best way

joecanti’s picture

Hi there,

Do you have a D6 port for this module? It's a great idea and just what Im looking for!

Many thanks for yout time to write this module, Joe

optalgin’s picture

Here is a D6 port,

Example how to use:
www.yoursite.com/latest/story - display the latest node of type story
www.yoursite.com/latest/letter - display the latest node of type letter

You can edit the value of $allowed_types if you need

latest_content.info

name = "Latest content"
description = "Redirect to the latest content"
core = "6.x"

latest_content.module


/**
* Implementation of hook_menu
*/

function latest_content_menu() {

  $items = array();

  $items['latest/%'] = array(
    'title' => t('Latest content'),
    'description' => t('Latest content of a given node type'),
    'page callback' => 'latest_content_redirect',
    'page arguments' => array(1),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );                                  

  return $items;
}

function latest_content_redirect($content_type) {
 
    // Restrict the redirection for given content types
    $allowed_types = array('letter', 'story');

    $node_type = strtolower($content_type);
    if (in_array($node_type, $allowed_types)) {
        $alias = 'content/'. $node_type;
 
       // Find latest letter
       $query = "SELECT nid FROM {node} WHERE type='%s' ORDER BY created DESC";
       $nid = db_result(db_query_range($query, $node_type, 0, 1));
 
       // Set alias and redirect
       path_set_alias('node/'.  $nid, $alias);
       drupal_goto($alias);
    }
    else {
       drupal_not_found();
    }
}

b.t.w
If you are using views module in your site, you can achive this functionality without the need for a custom module

joecanti’s picture

Thanks very much Optalgin,
I'll give this a go tomorrow - should be excellent for what i need. I am using Views yes, but I want it to re-direct to the actual node rather than a view display that node on a separate page because I have other views on the same page referencing the node ID.

Will post back here as how I get on,

Many thanks, Joe

optalgin’s picture

Good luck and let me know how it go

joecanti’s picture

Unfortunately it only works first time - when I go to latest/blog it goes to the latest blog, but when I create a new blog entry and do the same it goes to the previous blog. I think this is because path alias creates an alias for the first blog (at content/blog), which then is redirected the second time i go to 'latest/blog'.

When I delete the created alias and try again it works as expected - im just not sure how to stop path alias from creating an alias (although I've tried removing the automatic creation)

Can you think of any way to get around this problem and simply redirect to the latest blog?

Many thanks for your help,

Joe

optalgin’s picture

you have two paths

  1. latest/blog that activates the php code, updates the alias content/blog and redirect to it
  2. content/blog which is the alias to your content created in step 1.
    This path should not be used by "user", not so elegant.. I admit :-)

If you want to skip the aliasing and redirect to the node itself (i.e. node/123)
replace the lines

       // Set alias and redirect
       path_set_alias('node/'.  $nid, $alias);
       drupal_goto($alias);

with

       // Redirect
       drupal_goto('node/'.  $nid);
WorldFallz’s picture

Another way to do this if you have views installed, and many sites do, is just setup a page view-- set the items to 1 and sort by date posted descending.

joecanti’s picture

thanks Optalgin - just got round to testing this and it works great!

I use views yes, but wasnt sure if it was possible to redirect to the actual page, rather than display a view of the latest page...I have other views on the same page referencing the node ID (EG a nice little post-it style view for displaying the blog post date)

The module works great - thanks again!

Joe

optalgin’s picture

My pleasure :-)