I have been plugging at this for a few days now. I have a page with a calendar on it. This was originally in drupal 6 and I am writing a new module for 7. All has been good except for the outputting of the form on the frontend in 7. I am sure I am missing some right in front of me. No matter how many times I pour over this code I am not able to find what I am doing wrong.
Here is my module. I have cut out the extra functions and code.
springcalendar.module
<?php
// $Id$
/**
* Implementation of hook_menu().
*/
function springcalendar_menu() {
$items['calendar'] = array(
'title' => 'Calendars',
'page callback' => 'springcalendar_page',
// 'page arguments' => array('springcalendar_page_form'),
// 'access arguments' => array('access content'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Return rendered Calendar and table form
* with 'Remind me' feature.
* The function for calling from another custom modules.
*/
function springcalendar_page() {
drupal_add_js(drupal_get_path('module', 'springcalendar') . '/jquery_ui_datepicker/jquery-ui-1.7.3.custom.min.js');
drupal_add_js(drupal_get_path('module', 'springcalendar') . '/springcalendar.js');
drupal_add_css(drupal_get_path('module', 'springcalendar') . '/jquery_ui_datepicker/jquery-ui-1.7.3.custom.css');
drupal_add_css(drupal_get_path('module', 'springcalendar') . '/springcalendar.css');
return theme('springcalendar_page', array('form' => drupal_get_form('springcalendar_page_form')));
}
function springcalendar_page_form($form, &$form_state) {
$form = array();
$future_items = array();
$all_items = array();
$settings = variable_get('springcalendar', array()); // get the calendar settings
$pdf_icon = theme_image(array('path' => drupal_get_path('module', 'springcalendar') . '/images/PDFIcon.png', 'attributes' => array()));
// Get all events
$items = _springcalendar_get_items($settings['default_calendar']);
// echo '<pre>item: ' . print_r($items, true) . '</pre>'; exit;
// Sort them out for JS and future events
foreach ($items as $item) {
// echo '<pre>item: ' . print_r($item, true) . '</pre>';
$nid = $item->nid;
// echo '<pre>nid: ' . $nid . '</pre>';
$start_date = $item->field_item_date['und'][0]['value'];
// echo '<pre>start_date: ' . $start_date . '</pre>';
$date = explode('/', date('d/m/y', $start_date));
// echo '<pre>date: ' . print_r($date, true) . '</pre>';
// Structure of array: array[year][month][day][event]
// $all_events[$date[2]][$date[1]][$date[0]][$nid] = (array)$item;
if (isset($item->field_more_information['und'][0]['value']) && ($item->field_more_information['und'][0]['value'] == 'External Link')) {
$str_icon = l($item->field_external_link['und'][0]['title'], $item->field_external_link['und'][0]['url'], array('attributes' => array('target' => '_blank')));
$str_link = $item->field_external_link['und'][0]['url'];
} elseif (isset($item->field_more_information['und'][0]['value']) && ($item->field_more_information['und'][0]['value'] == 'PDF')) {
$str_link = $item->field_pdf['und'][0]['uri'];
$str_icon = l($pdf_icon, $item->field_pdf['und'][0]['uri'], array('html' => true, 'attributes' => array('target' => '_blank')));
} else {
$str_link = '';
$str_icon = '';
}
/*
* This is so it matches the same format as the old one
*/
$arr_item = array(
'date' => $item->field_item_date['und'][0]['value'],
'link' => $str_link,
'icon' => $str_icon,
'nid' => $item->nid,
'title' => $item->title,
'body' => $item->body['und'][0]['safe_value']
);
$all_events[$date[2]][$date[1]][$date[0]][$nid] = $arr_item;
// Format the date value for JS
$all_events[$date[2]][$date[1]][$date[0]][$nid]['date'] = date('d/m/Y', $start_date);
// -1 Because today's events also should be showed
if ($start_date > strtotime('-1 day')) {
$future_events[$nid] = $arr_item;
}
}
// echo '<pre>all_events: ' . print_r($all_events, true) . '</pre>';
// echo '<pre>future events: ' . print_r($future_events, true) . '</pre>'; exit;
drupal_add_js(array('events' => $all_events, 'raw_events' => $items, 'date' => array('year' => date('y'), 'month' => date('m'))), 'setting');
$form['#events'] = $future_events;
$form['events']['#tree'] = TRUE;
foreach ($future_events as $nid => $event) {
$form['events'][$nid] = array(
'#tree' => TRUE,
'on' => array(
'#type' => 'checkbox',
'#title' => '',
),
'prior' => array(
'#type' => 'textfield',
'#size' => 2,
'#title' => '',
)
);
}
$form['email'] = array(
'#type' => 'textfield',
'#title' => '',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit')
);
// $form['theme'] = 'springcalendar_page_form';
return $form;
}
function theme_springcalendar_page_form($variables) {
echo '<pre>$variables: ' . print_r($variables, true) . '</pre>'; exit;
$form = $variables['form'];
// echo '<pre>theme form: ' . print_r($form, true) . '</pre>'; exit;
// echo '<pre>form events: ' . print_r($form['events'], true) . '</pre>'; exit;
$rows = array();
foreach (element_children($form['form']['events']) as $nid) {
// echo '<pre>nid: ' . print_r($nid, true) . '</pre>'; exit;
$event = &$form['form']['events'][$nid];
// echo '<pre>event: ' . print_r($event, true) . '</pre>'; exit;
// echo '<pre>event: ' . print_r($form['form']['#events'][$nid], true) . '</pre>';
if ($form['form']['#events'][$nid]['link'] != '') {
$str_link = l($form['form']['#events'][$nid]['title'], $form['form']['#events'][$nid]['link'], array('attributes' => array('target' => '_blank')));
} else {
$str_link = $form['form']['#events'][$nid]['title'];
}
$rows[] = array(
date('d/m/y', $form['form']['#events'][$nid]['date']),
$str_link,
array('data' => $form['form']['#events'][$nid]['icon'], 'class' => 'info_links_cell'),
// '<div class = "on-checkbox">' . drupal_render($event['on']) . '</div><div class = "prior-field">' . drupal_render($event['prior']) . '</div>' . t('days prior')
'<div class = "on-checkbox">' . drupal_render($event['on']) . '</div><div class = "prior-field">' . drupal_render($event['prior']) . '</div>' . t('days prior')
);
}
$rows[] = array(array('data' => '', 'class' => 'date-empty'), array('data' => '<span class = "text-block">enter your email address</span>' . drupal_render($form['email']) . drupal_render($form['submit']), 'colspan' => 2));
$header = array(array('data' => t('Date'), 'class' => 'date'), array('data' => t('Event'), 'class' => 'event'), array('data' => t('Information'), 'class' => 'info_links'), array('data' => t('Remind me'), 'class' => 'remind-me'));
// echo '<pre>rows: ' . print_r($rows, true) . '</pre>'; exit;
// echo '<pre>rows: ' . drupal_render($form) . '</pre>'; exit;
return theme('table', $header, $rows, array('id' => 'events-remeber', 'cellspacing' => 3)) . drupal_render($form);
}
/**
* Implementation of hook_theme().
*/
function springcalendar_theme() {
return array(
'springcalendar_page' => array(
'variables' => array('form' => NULL),
'path' => drupal_get_path('module', 'springcalendar'),
'template' => 'springcalendar-page',
),
'springcalendar_page_form' => array(
'variables' => array('form' => NULL),
// 'path' => drupal_get_path('module', 'springcalendar'),
// 'function' => 'theme_springcalendar_page_form',
'render element' => 'form',
),
);
}
Here is the template:
springcalendar_page.tpl.php
<?php
// $Id$
$settings = variable_get('springcalendar', array());
?>
<div id = "calendar-wrap">
<div id="calendar-ui">
</div>
<div id="calendar-right-column">
<h5>Calendar instructions</h5>
<?php if(isset($settings['calendar_instructions'])): ?>
<?php echo $settings['calendar_instructions']; ?>
<?php endif; ?>
<div id="selected-date">
<p id = "selected-text">Selected date</p>
<div id="selected-events"></div>
</div>
</div>
<div class="clear"></div>
<?php echo dpm($form); ?>
<?php //print $form; ?>
<?php // print drupal_render($form); ?>
<?php echo '<pre>form: ' . print_r($form, true) . '</pre>';?>
<?php // print drupal_render($form); ?>
<?php //print render($form); ?>
</div>
No matter what I have done for the last who knows how many hours now the code is not calling the theme function theme_springcalendar_page_form()
I can get the theme to call if I use return them('springcalendar_page_form', form); at the end of function springcalendar_page_form()
however this does not appear to generate the form correctly and is because I think its not being processed correctly. This point the code is not calling the theme_ function.
Output of the form should look like the one featured here http://aquariusplatinum.com/investor_calendar
Any help with where I am going wrong would be appreciated.
{UPDATE}
This is now solved. A colleague helped me solve this. I was not far off. The cause of my problem was that I was mixing the names of some of the default drupal naming conventions and I was using them rather than over riding them. The solution was in summary.
change
function springcalendar_page() {
drupal_add_js(drupal_get_path('module', 'springcalendar') . '/jquery_ui_datepicker/jquery-ui-1.7.3.custom.min.js');
drupal_add_js(drupal_get_path('module', 'springcalendar') . '/springcalendar.js');
drupal_add_css(drupal_get_path('module', 'springcalendar') . '/jquery_ui_datepicker/jquery-ui-1.7.3.custom.css');
drupal_add_css(drupal_get_path('module', 'springcalendar') . '/springcalendar.css');
return theme('springcalendar_page', array('form' => drupal_render(drupal_get_form('springcalendar_page_form'))));
}
to
function springcalendar_page() {
drupal_add_js(drupal_get_path('module', 'springcalendar') . '/jquery_ui_datepicker/jquery-ui-1.7.3.custom.min.js');
drupal_add_js(drupal_get_path('module', 'springcalendar') . '/springcalendar.js');
drupal_add_css(drupal_get_path('module', 'springcalendar') . '/jquery_ui_datepicker/jquery-ui-1.7.3.custom.css');
drupal_add_css(drupal_get_path('module', 'springcalendar') . '/springcalendar.css');
$form = drupal_get_form('springcalendar_page_calander_form');
return theme('springcalendar_page', array('form' => $form));
}
and
function springcalendar_page_form($form, &$form_state) {
to
function springcalendar_page_calander_form($form, &$form_state) {
Then on my theme function
function springcalendar_theme() {
return array(
'springcalendar_page' => array(
'variables' => array('form' => NULL),
'path' => drupal_get_path('module', 'springcalendar'),
'template' => 'springcalendar-page',
),
'springcalendar_page_form' => array(
'variables' => array('form' => NULL),
'render element' => 'form',
),
);
changed too
function springcalendar_theme() {
return array(
'springcalendar_page' => array(
'variables' => array('form' => NULL),
'path' => drupal_get_path('module', 'springcalendar'),
'template' => 'springcalendar-page',
),
'springcalendar_page_form' => array(
'variables' => array('form' => array()),
'function' => 'springcalendar_form_theme',
),
);
}
Then on the template page
<?php echo dpm($form); ?>
changed to
<?php echo theme('springcalendar_page_form', array('form' => $form)); ?>
I hope this helps others in the future.
Comments
At a really quick glance,
At a really quick glance, $form['theme'] should be $form['#theme'].
Contact me to contract me for D7 -> D10/11 migrations.
I have it commented out
I have it commented out because with or without it. It makes no difference. Still is not calling the function theme_springcalendar_page_form() even though it is defined in springcalendar_theme().
It should make a difference
It should make a difference if you include the # as I showed in my last post.
Also, have you cleared your cache?
Contact me to contract me for D7 -> D10/11 migrations.
Since you've marked this as
Since you've marked this as solved, you should post the solution for others.
Contact me to contract me for D7 -> D10/11 migrations.
I have. Read the updated
I have. Read the updated original post.
Good to see :)
Good to see :)
Contact me to contract me for D7 -> D10/11 migrations.
I would stay away from
I would stay away from
$form['#theme']if I were you (see the comments on theme('form')) and instead use'#theme'on elements in the form. See my blog post about theming forms with tables for an example of this.