Posted by bdornbush on November 30, 2012 at 11:09pm
I needed to create some menu entries where the entries pointed to this month's, next month's, and the month after that's calendars. I wanted the calendar entry to use the month's name as the menu item, and I wanted the entries to automatically update as the months progressed.
I got the menu items to be placed on the primary menu, but I couldn't figure out how to position them exactly where I wanted them. It was easy enough to move the entries around using the menu screen.
Here is the custom module I came up with:
<?php
// $Id$
/**
* @file
* Creates menu entries for this month, next month, and next month
*
* Creates menu entries under Calendar that will display the current month, next month, and the month after that
* hwd 2012-11-29
*/
/**
* Implementation of hook_menu().
*/
function menu_calendar_month_menu() {
$items['classes/thismonth'] = array(
'title' => 'thismonth',
'description' => "Go to this month's menu",
'title callback' => 'menu_calendar_month_this_name',
'page callback' => 'menu_calendar_month_this_date',
'access callback' => TRUE,
'menu_name' => 'primary-links',
);
$items['classes/nextmonth'] = array(
'title' => 'nextmonth',
'description' => "Go to next month's menu",
'title callback' => 'menu_calendar_month_next_name',
'page callback' => 'menu_calendar_month_next_date',
'access callback' => TRUE,
'menu_name' => 'primary-links',
);
$items['classes/secondmonth'] = array(
'title' => 'secondmonth',
'description' => "Go to second month's menu",
'title callback' => 'menu_calendar_month_second_name',
'page callback' => 'menu_calendar_month_second_date',
'access callback' => TRUE,
'menu_name' => 'primary-links',
);
return $items;
}
/**
* Title callback.
*/
function menu_calendar_month_this_name() {
$month_name = new DateTime();
$month_name_m = $month_name->format('F');
return $month_name_m;
}
function menu_calendar_month_next_name() {
$next_month = new DateTime();
$next_month->modify('first day next month');
$next_month_m = $next_month->format('F');
return $next_month_m;
}
function menu_calendar_month_second_name() {
$second_month = new DateTime();
$second_month->modify('first day +2 month');
$second_month_m = $second_month->format('F');
return $second_month_m;
}
/**
* Page callback.
*/
function menu_calendar_month_this_date() {
$this_month = new DateTime();
$this_month_d = $this_month->format('Y-m');
$url = "calendar/$this_month_d";
drupal_goto($url);
}
function menu_calendar_month_next_date() {
$next_month = new DateTime();
$next_month->modify('first day next month');
$next_month_d = $next_month->format('Y-m');
$url = ("calendar/$next_month_d");
drupal_goto($url);
}
function menu_calendar_month_second_date() {
$second_month = new DateTime();
$second_month->modify('first day +2 month');
$second_month_d = $second_month->format('Y-m');
$url = ("calendar/$second_month_d");
drupal_goto($url);
}