Format Drupal Calendar Module Mini Calendar Title View/Block

Theming the Date and Calendar modules can sometimes be a little tricky. I wanted to change the title display for a "mini" calendar in a block from "Month" to "Month YYYY" (e.g., "October 2010"). In order to do this you need to first learn how to override a theme function.

Now that you're up to par on theme overrides, we can override this theme function: /modules/date/theme/theme.inc: theme_date_nav_title. You'll have something that looks like this in Drupal 6.x.

In my case, I just had to adjust the ternary IF statement to always display 'F Y' as the date format, but you can adjust whatever you wish here.

<?php
/**
* Theme the calendar title
*/
function [your_template_name]_date_nav_title($granularity, $view, $link = FALSE, $format = NULL) {
  switch (
$granularity) {
    case
'year':
     
$title = $view->date_info->year;
     
$date_arg = $view->date_info->year;
      break;
    case
'month':
     
$format = !empty($format) ? $format : (empty($view->date_info->mini) ? 'F Y' : 'F Y');
     
$title = date_format_date($view->date_info->min_date, 'custom', $format);
     
$date_arg = $view->date_info->year .'-'. date_pad($view->date_info->month);
      break;
    case
'day':
     
$format = !empty($format) ? $format : (empty($view->date_info->mini) ? 'l, F j Y' : 'l, F j');
     
$title = date_format_date($view->date_info->min_date, 'custom', $format);
     
$date_arg = $view->date_info->year .'-'. date_pad($view->date_info->month) .'-'. date_pad($view->date_info->day);
      break;
    case
'week':
     
$format = !empty($format) ? $format : (empty($view->date_info->mini) ? 'F j Y' : 'F j');
     
$title = t('Week of @date', array('@date' => date_format_date($view->date_info->min_date, 'custom', $format)));
     
$date_arg = $view->date_info->year .'-W'. date_pad($view->date_info->week);
      break;
  }
  if (!empty(
$view->date_info->mini) || $link) {
   
// Month navigation titles are used as links in the mini view.
   
$attributes = array('title' => t('View full page month'));
   
$url = date_real_url($view, $granularity, $date_arg, TRUE);
    return
l($title, $url, array('attributes' => $attributes));
  }
  else {
    return
$title;
  }
}
?>

Awesome. Very helpful.

Awesome. Very helpful.

Nice article, thanks for the

Nice article, thanks for the information.

Thanks you very much ! This

Thanks you very much ! This is very helpful !

For Drupal 7 version, this is almost the same. The name of the function is a bit different.

The code below has the same result as the code posted by Patrick

<?php
function yourthemename_date_nav_title($params) {
 
$granularity = $params['granularity'];
 
$view = $params['view'];
 
$date_info = $view->date_info;
 
$link = !empty($params['link']) ? $params['link'] : FALSE;
 
$format = !empty($params['format']) ? $params['format'] : NULL;
  switch (
$granularity) {
    case
'year':
     
$title = $date_info->year;
     
$date_arg = $date_info->year;
      break;
    case
'month':
     
$format = !empty($format) ? $format : (empty($date_info->mini) ? 'F Y' : 'F Y');
     
$title = date_format_date($date_info->min_date, 'custom', $format);
     
$date_arg = $date_info->year .'-'. date_pad($date_info->month);
      break;
    case
'day':
     
$format = !empty($format) ? $format : (empty($date_info->mini) ? 'l, F j Y' : 'l, F j');
     
$title = date_format_date($date_info->min_date, 'custom', $format);
     
$date_arg = $date_info->year .'-'. date_pad($date_info->month) .'-'. date_pad($date_info->day);
      break;
    case
'week':
       
$format = !empty($format) ? $format : (empty($date_info->mini) ? 'F j Y' : 'F j');
     
$title = t('Week of @date', array('@date' => date_format_date($date_info->min_date, 'custom', $format)));
       
$date_arg = $date_info->year .'-W'. date_pad($date_info->week);
        break;
  }
  if (!empty(
$date_info->mini) || $link) {
     
// Month navigation titles are used as links in the mini view.
   
$attributes = array('title' => t('View full page month'));
     
$url = date_pager_url($view, $granularity, $date_arg, TRUE);
    return
l($title, $url, array('attributes' => $attributes));
  }
  else {
    return
$title;
  } 
}
?>

Very cool.... thanks for the

Very cool.... thanks for the update!

Very nice. Hope to impliment

Very nice. Hope to impliment it on my drupal site.

Nice trick to override a

Nice trick to override a theme function.

Not sure if this is the right

Not sure if this is the right place to ask this semi-related question, but the mini calednar block doesn't make the "Days" clickable. Do I need to do something to the view or overwriting the theme? I'm using Calendar module version 7.x-3.0.

I've not been to with it in

I've not been to with it in the new 7x calendar, but as I recall the days are only clickable when there's an event on that day... then it should be. If you want to make them all clickable, then you would do it in a theme override.

Where do you put this

Where do you put this function?

It goes in your theme's

It goes in your theme's template.php

Great example thanks. I also

Great example thanks. I also found this page helpful as a more general overview of theme overrides ... http://drupalsn.com/learn-drupal/drupal-tutorials/drupal-theme-function-...

Cheers :-) Dave

Hello all! First, THANKS for

Hello all! First, THANKS for all who posted this code! I used Jibus's code for Drupal 7 which was awesome. Using this code I also altered the title for "Week of ______" which now says simply "March 3 2013 - March 10 2013" since I felt that was more useful to a visitor. The bit of code I changed was:

Original (drupal 7):
$title = t('Week of @date', array('@date' => date_format_date($date_info->min_date, 'custom', $format)));

Changed to:
$title = t('@startdate - @enddate', array('@startdate' => date_format_date($date_info->min_date, 'custom', $format), '@enddate' => date_format_date($date_info->max_date, 'custom', $format)));

THIS IS NOT CHECKED so use at own risk...I'm not a coder by any means so if someone who IS a coder can suggest something better by all means share! I just tried some things and this worked for me. Hope it helps someone else who might want this option for the Week calendar. Also, for newbs who aren't sure where to put Jibus's code, I put it into "template.php" in my theme folder" and once uploaded I cleared cache.

Happy Drupalling everyone! SW

This is a better way for

This is a better way for Drupal 6

/**
* Theme the calendar title
* #RA:30.4.13
*/
function [your theme]_date_nav_title($granularity, $view, $link = FALSE, $format = NULL) {
 
  $title = $view->display['default']->display_options['title'];
  switch ($granularity) {
    case 'year':
      $title .= ' - ' . $view->date_info->year;
      $date_arg = $view->date_info->year;
      break;
    case 'month':
      $format = !empty($format) ? $format : (empty($view->date_info->mini) ? 'F Y' : 'F Y');
      $title .= ' - ' . date_format_date($view->date_info->min_date, 'custom', $format);
      $date_arg = $view->date_info->year .'-'. date_pad($view->date_info->month);
     
      break;
    case 'day':
      $format = !empty($format) ? $format : (empty($view->date_info->mini) ? 'l, F j Y' : 'l, F j');
      $title .= ' - ' .  date_format_date($view->date_info->min_date, 'custom', $format);
      $date_arg = $view->date_info->year .'-'. date_pad($view->date_info->month) .'-'. date_pad($view->date_info->day);
      break;
    case 'week':
      $format = !empty($format) ? $format : (empty($view->date_info->mini) ? 'F j Y' : 'F j');
      $title .= ' - ' .  t('Week of @date', array('@date' => date_format_date($view->date_info->min_date, 'custom', $format)));
      $date_arg = $view->date_info->year .'-W'. date_pad($view->date_info->week);
      break;
  }
 
  drupal_set_title($title);
  if (!empty($view->date_info->mini) || $link) {
    // Month navigation titles are used as links in the mini view.
    $attributes = array('title' => t('View full page month'));
    $url = date_real_url($view, $granularity, $date_arg, TRUE);
    return l($title, $url, array('attributes' => $attributes));
  }
  else {
    return $title;
  }
}

Rajab Natshah
rajab.natshah.com

Good tip, thanks!

Good tip, thanks!

Nice tip Thanks

Nice tip Thanks