I use this module all the time, but the calendars don't look all that great. For instance, I cannot assign a different style to the caption (month name), the day headers, etc.

This is because the output of the calendar generation uses theme('table', $rows), where the rows are all table rows incluing the one for the caption, the headers, etc.

I have seen a more powerful theming function that takes rows, caption, and headers and generates HTML that includes table caption tags, th tags, etc. If this function were used, then the ability to style and theme the calendar display would be greatly improved.

CommentFileSizeAuthor
#7 archive_6x.patch3.58 KBSusurrus
#5 archive.patch4.05 KBSusurrus

Comments

Susurrus’s picture

Title: Use a theming call for table rendering that emits caption, th, etc. » Move calendar block into theme function

I'm not sure in your last paragraph if you have existing code you'd like me to use or I should write my own theming function for this. If you know PHP, you're also encouraged to commit a patch for this feature.

Susurrus’s picture

Okay, well, I've looked at the code, and it's already in a theming function, so you're welcome to override this function in your own theme. I agree that this function could be improved more so that the actualy theming is separated from the data (an array of days with their content would be passed to a theming function), but I don't consider it a priority.

jrisberg’s picture

I know some PHP but not enough of the Drupal API, so I'm sending you this code rather than checking it in.

I have a better explanation of what I wanted now: in the function theme_archive_block_calendar($timestamp), there is a call to theme('table', ...). This is fine, but the table generation also support creating a caption and a header, if additional args are specified. So in the version that I'm proposing, there is call at the end that reads: return theme('table', $day_titles, $rows, array(), $month_title);

The full code is :

function theme_archive_block_calendar($timestamp) {
  $the_date = explode(' ', format_date($timestamp, 'custom', 'F Y n t'));
  $title = $the_date[0] .' '. $the_date[1];
  $year = $the_date[1];
  $month = $the_date[2];
  $num_days = (int)$the_date[3];
  
  $date = _archive_date('all', $year, $month);
  
  $month_title = '';
  if (in_array($month, $date->months)) {
    $month_title = l($title, _archive_url('all', $date, $year, $month), array('title' => format_plural($date->months[format_date($timestamp, 'custom', 'n')], '1 post', '@count posts')));
  }
  else {
    $month_title = $title;
  }
  
  // Build the week starting with 
  $first_day_of_week = variable_get('date_first_day', 0);
  $week = array(t('Sun'), t('Mon'), t('Tue'), t('Wed'), t('Thu'), t('Fri'), t('Sat'));
  $day_titles = array();
  for ($i = $first_day_of_week; $i < $first_day_of_week + 7; $i++) {
    $day_titles[] = $week[$i%7];
  }
   
  // This is inefficient but necessary to determine the start of the month:
  // We use gmdate the first time so that we don't apply the user's timezone twice
  $start_year = format_date($timestamp, 'custom', 'Y');
  $start_month = format_date($timestamp, 'custom', 'm');
  $start = gmmktime( 0, 0, 0, (int)$start_month, 1, (int)$start_year );
  $weekday = array_search(gmdate('D', $start), $day_titles);

  $days_row = array();  
  $rows[] = array();
  
  // From http://www.theadminzone.com/forums/showthread.php?t=17490
  for ($i = 1-$weekday;$i <= ceil(($weekday+$num_days)/7)*7;$i++) {
    if ($i > 0) {
      if (array_key_exists($i, $date->days)) {
        $days_row[] = l($i, _archive_url('all', $date, $year, $month, $i), array('title' => format_plural($date->days[$i], '1 post', '@count posts')));
      }
      else if ($i <= $num_days) {
        $days_row[] = $i;
      }
      else {
        $days_row[] = '';
      }
      
      // Add the week table row we just created if we've finished it   
      if (($i+$weekday)%7 == 0) {
        $rows[] = $days_row;
        $days_row = array();
      }
    }
    else {
      $days_row[] = '';
    }
  }
  return theme('table', $day_titles, $rows, array(), $month_title);
}

Then I can use a stylesheet with rules like the following, and style the header, the caption, and the rows differently.

.block-archive caption {
	color: #000;
	font-size: 1.1em;
	font-weight: bold;
}

.block-archive th {
	text-align: center;
	color: #222;
	width: 30px;
	padding: 3px 0px 3px 0px;
}

.block-archive td {
	text-align: center;	
	width: 30px;
	padding: 3px 0px 3px 0px;
}

Hope that you find this of interest. This was the first time that I have traced through the themeing logic of Drupal.

Susurrus’s picture

Version: 5.x-1.7 » 5.x-1.x-dev

While I'm doing this I would like to refactor the theming function to better uphold MVC principles and so the theme function should be passed all the date formatting information it needs and then it just displays it without any extra logic in there. Currently, this is not the case. I have been developing a new version of this function as well as hook_block() obviously, but it wouldn't hurt if you might take a stab at it. Them grad apps are a tad more important right now...

Susurrus’s picture

Status: Active » Needs review
StatusFileSize
new4.05 KB

Patch is against 5.x-1.x-dev since that wraps up a ton of changes that will be rolled out as the next version. Download that, apply this patch, and tell me if it works for ya.

jrisberg’s picture

Thanks for the reply. Glad to see that you have incorporated my change in the patch. I believe we are on the same wavelength.

* Regarding basic testing, unfortunately I am running 6.0 RC1, so I can't test your patch effectively. Community: can someone else try this?

* Regarding refactoring the function including hook_block()... I don't have the Drupal skill set to do that. I know enough PHP, but am still learning the Drupal API.

Good luck on the grad apps!

Susurrus’s picture

Version: 5.x-1.x-dev » master
StatusFileSize
new3.58 KB

Rerolled against HEAD.

Susurrus’s picture

Title: Move calendar block into theme function » Revise theme_table() call in theme_archive_block_calendar() and separate out logic more
Status: Needs review » Fixed

Applied to head and 5.x

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

jmpacquet’s picture

Component: Code » Block

I applied the patch on top of my (already patched for PostgreSQL) archive.module which was originally a 5.7-1.9 version.
It works for me.