The simple monthly archive

Last modified: June 22, 2009 - 00:24

With Views, it's easy to do a 'Monthly Archive'. This view creates a block which will contain a list of months including the number of posts per month; if there are enough months it will contain a [more] link. Each link goes to a simple teaser view for that month.

Import this view:

<?php
$view
= new stdClass();
 
$view->name = 'archive';
 
$view->description = 'Monthly Archive';
 
$view->access = array (
);
 
$view->view_args_php = '';
 
$view->page = TRUE;
 
$view->page_title = 'Monthly Archive';
 
$view->page_header = '';
 
$view->page_header_format = '1';
 
$view->page_footer = '';
 
$view->page_footer_format = '1';
 
$view->page_empty = '';
 
$view->page_empty_format = '1';
 
$view->page_type = 'teaser';
 
$view->url = 'archive';
 
$view->use_pager = TRUE;
 
$view->nodes_per_page = '10';
 
$view->block = TRUE;
 
$view->block_title = 'Archive';
 
$view->block_header = '';
 
$view->block_header_format = '1';
 
$view->block_footer = '';
 
$view->block_footer_format = '1';
 
$view->block_empty = '';
 
$view->block_empty_format = '1';
 
$view->block_type = 'list';
 
$view->nodes_per_block = '5';
 
$view->block_more = TRUE;
 
$view->block_use_page_header = FALSE;
 
$view->block_use_page_footer = FALSE;
 
$view->block_use_page_empty = FALSE;
 
$view->sort = array (
    array (
     
'tablename' => 'node',
     
'field' => 'created',
     
'sortorder' => 'DESC',
     
'options' => 'normal',
    ),
  );
 
$view->argument = array (
    array (
     
'type' => 'monthyear',
     
'argdefault' => '5',
     
'title' => '%1',
     
'options' => '',
     
'wildcard' => '',
     
'wildcard_substitution' => '',
    ),
  );
 
$view->field = array (
    array (
     
'tablename' => 'node',
     
'field' => 'title',
     
'label' => '',
     
'handler' => 'views_handler_field_nodelink',
     
'options' => 'link',
    ),
  );
 
$view->filter = array (
  );
 
$view->exposed_filter = array (
  );
 
$view->requires = array(node);
 
$views[$view->name] = $view;
?>

And use this code in your template.php (You may have to adjust this slightly if using a non-phptemplate theme). This theme will make the summary look more like a menu.

<?php
function phptemplate_views_summary_archive($view, $type, $level, $nodes, $args) {
  foreach (
$nodes as $node) {
   
$list .= '<li class="leaf">' . views_get_summary_link($view->argument[$level]['type'], $node, $view->real_url) . "</li>\n";
  }

  if (
$list) {
    return
"<div class='menu'><ul>$list</ul></div>";
  }
}
?>

Some Notes:

  1. To prevent that unpublished nodes or non-blog nodes are taken into account, the view should also filter on:
    Node: Published = Yes
    Node: Type = One of 'Blog entry'
  2. Archive as a Drop Down List

    We modified the function to draw the archive list as a drop down, and works sweet.

    We still havent managed to find how to get the actual url that each entry should point to into the VALUE='' field ... so if anyone knows we would appreciate the addition to the page...

    eg. <OPTION VALUE='archive/200803'>

    1. <?php
      function phptemplate_views_summary_archive($view, $type, $level, $nodes, $args) {
       
      $count = 1;
        foreach (
      $nodes as $node) {
         
      $list .= "<OPTION VALUE='".$count."'>". views_get_summary_link($view->argument[$level]['type'], $node, $view->real_url) . ': ' . $node->num_nodes . ' items';
        
      $count += 1;
        }

        if (
      $list) {
          return
      "<div class='menu'><FORM NAME='myform' onChange='followlink()'><SELECT NAME='mylist'>$list</SELECT></FORM></div>";
        }
      }
      ?>

      The Drupal 6 version of Views comes with a default view that achieves this functionality.

Great view!; but how can I

timnl - October 28, 2008 - 09:13

Great view!; but how can I filter this block, so it only display's the nodes from the user from who the blog is?
The url for the pages is like this http://domain.com//weblog,

then i use this handeling code to get the right user id:

global $user;
$args[0] = $user->uid;
if (function_exists('ikvader_pathparts')) {
$pathparts = ikvader_pathparts();
$args[0] = $pathparts['user'];
}

but then the filter is right, but the links to the archive pages is getting wrong. like archive/%2520081028
how can i prevent this?

Thanks, Tim

What do I need to change for

jamesmcd - July 1, 2009 - 15:13

What do I need to change for this to work in Views 2?

At present I am getting this message:

'You are importing a view created in Views version 1. You may need to adjust some parameters to work correctly in version 2."

Import Error

cbh1974 - July 4, 2009 - 17:57

I was struggling with importing this for most of the morning and I thought I would save someone a lot of grief by letting you know how I got it to work.

It is not rocket science but you have to remove the PHP tags from this to import into Views correctly. So delete the

<?php
and the closing
?>
and it should import fine. I am using Drupal 5.x.

Thank you,

Clark

 
 

Drupal is a registered trademark of Dries Buytaert.