Hello,

I've scoured drupal.com for info on this but haven't found anything exactly like what I'm looking to do in views:

A block containing a list of year links to content. Click a year, get a list of month links, click a month get a list of day links. Each click results in the view page being refreshed with the content appropriate for that date selection. Effectively a year/month/day drill down.

Using D6, and Views 2, here is what I already have:

Views: Block: Arguments:
- Node Created Year : Summary, sorted descending
- Node Created Month : Summary, sorted descending
- Node Created Day : Summary, sorted descending

It works by showing the year, but when I click a year, I don't see a list of months... make sense?

I also want my path to be: news/year/month/day not: news/yearmonth (which is a common suggestion)

Thanks for any help to point me in the right direction!!

Comments

barrett’s picture

It's not exactly what you want, but I'd start looking at how the Archive module does things. Conceptually you and they are working along the same lines.

Are you looking to implement this in a page or a block? The title says block but then you refer to a page later on.

b.l.mak’s picture

Hello,
I was looking for this solution too. It seems like a common need. So I thought I would post the somewhat hackish solution I came up with (I am still a drupal newB, so it is likely that this isn't a great approach):

1. Copy the archive view that ships with drupal, as a starting point
2. Open the block display
3. In the arguments section:

  • add the arguments 1. Node: Created Year; 2. Node: Month.
  • choose Summary, sorted descending
  • add the style list to both
  • make sure the Year argument is before the Month argument
  • Delete the Node: Created Year + Month

4. give your block a machine name (e.g., news_archive)
5. Add the following to your template's template.php

function yourtheme_preprocess_block(&$vars) {

/*
* get years from [your_view] and pass as parameter to [your_archive] block
* to use later to create accordion of links
* you have to render the view in the block's preprocess to be able
* to get to the actual years, and not just have the view field metadata
*/

  if($vars['block']->delta == 'your_archive_view_display_name') {
    $viewName = 'your_archive_view';
    $display_id = 'your_archive_view_display_name';
    $view = views_get_view($viewName);
    $view->set_display($display_id);
    $view->render();

    $years = array();
    foreach ($view->result as $result) {
      $year = $result->created_year;
      if (in_array((string)$year, $years, TRUE) == FALSE) {
        array_push($years, $result->created_year);
      }
    }

  $vars['block']->archive_years = $years;

  }
}

6. Then add something along the lines of the following in tpl file for that block. I am using the JQuery UI Accordion function to create the drop-down lists of months, after someone has clicked on a year. Though another UI approach I'm sure would work just as well.

<div id="archive_accordion" class="ui-accordion">

  <?php
  //will use the same view info as in the preprocess
  $viewName = 'your_archive_view';
  $display_id = 'your_block_name';
  $years = $block->archive_years; //get the passed parameter containing the array of years

  foreach ( $years as $year) {
    print '<h3><a href="#">'. $year . '</a></h3>';
    print '<div>';
    // but this time you will embed an archive view for each year in the block. 
    //i.e., actually print out a view with each year that was in the passed array
    print views_embed_view($viewName, $display_id, $year);
    print '</div>';
  }
?>

</div>

Conceivably, you could just add the day argument, and add get the month variables in the preprocess, and repeat to be able to drill another level for the Created Day.

subu.purohit’s picture

Hi mak,

I am not sure where to put step 6 code.
and can you please explain what is "your_archive_view" and "your_archive_view_display_name" in step 5

Thanks in advance.

b.l.mak’s picture

"your_archive_view" = the "View Name" you gave your view when you created (i.e., the machine name of the view you created). You did this in step 1 when you cloned the Archive view that ships with Drupal. Basically to embed a view you are calling that view by its name. This can be any type of view. We just happened to be creating an "archive" with our view.

"your_archive_view_display_name" = the name of the block display that you added to the above view (step 2). You can define the name of the display by opening that block/display within your view, and then under Basic Settings: Machine Name give that block a name. Otherwise it will be some generic name applied by drupal (e.g., Block_1 or something).

subu.purohit’s picture

Thanks mak,

My view name "testarchive" and and block display name is "test_archive". However I couldn't find Machine name in basic setting and I am assuming Display name as Machine name (I am using Drupal 6.24).
Now I have putted step 5 code in my theme's template.php.
Now where to put step 6 code.

qqboy’s picture

dtabach’s picture

I followed the instructions but I'm getting this error:

Notice: Undefined variable: block in include() (line 6 of /Applications/MAMP/htdocs/mysite/sites/all/themes/mytheme/views-view--my-archive-view--block-1.tpl.php).

Durval Tabach

seacoastcreates’s picture

I found Views nested Accordion to work with this type of Year to Month drill down. The key was to include Content: Post Date as a field as many times as there are levels in your archive. For me, it was only twice for year and month. Then, I set a custom date format for each of these field types: Y for Year and F for Month. Be sure to group each level by the appropriate Group field number. With this approach, contextual filters aren't necessary.