Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By PrincessOfAngels on
I have a custom where I need to render a calendar, but only shows the whole week of the current week.
I've added an element for the date where I've set the default value to something like below (actual code):-
$form['container']['date'] = array(
'#type' => 'hidden',
'#default_value' => $default_date,
);
My button is ajax and the type is just a button. These buttons are like previous month, next month, previous week and next week. Like below (actual code):-
function booking_calendar_form($form, &$form_state) {
$selected_date = 'today';
if (isset($form_state['values']['date']) && !empty($form_state['values']['date'])) {
$selected_date = strtotime($form_state['values']['date']);
if (isset($form_state['triggering_element']['#name'])) {
if ($form_state['triggering_element']['#name'] == 'previous_month') {
$selected_date = strtotime('-1 month', $selected_date);
}
elseif ($form_state['triggering_element']['#name'] == 'next_month') {
$selected_date = strtotime('+1 month', $selected_date);
}
elseif ($form_state['triggering_element']['#name'] == 'previous_week') {
$selected_date = strtotime('-1 week', $selected_date);
}
elseif ($form_state['triggering_element']['#name'] == 'next_week') {
$selected_date = strtotime('+1 week', $selected_date);
}
}
$selected_date = format_date($selected_date, 'custom', 'Y-m-d');
}
elseif (isset($params['date']) && !empty($params['date'])) {
$selected_date = $params['date'];
}
$selected_date = $default_date;
}
When the ajax runs, the hidden type value emptied. Not sure what's wrong with my code. Here is how I render the buttons (actual code):-
$form['navigation']['prev_month'] = array(
'#type' => 'button',
'#value' => t('Previous month'),
'#name' => 'previous_month',
'#ajax' => array(
'submitter' => FALSE,
'callback' => 'calendar_ajax_callback',
'event' => 'mousedown',
'wrapper' => 'container-wrapper',
'method' => 'replace',
'effect' => 'fade',
),
);
Form output/container:-
$form['container'] = array(
'#type' => 'container',
'#prefix' => '<div id="container-wrapper">',
'#suffix' => '</div>',
);
$form['container']['date'] = array(
'#type' => 'hidden',
'#default_value' => $default_date,
);
$form['container']['header'] = array(
'#markup' => $header_navigation,
);
$form['container']['calendar_display'] = array(
'#type' => 'markup',
'#markup' => $output,
);
Returning ajax callback:-
function calendar_ajax_callback(&$form, &$form_state) {
$form = booking_calendar_form($form, $form_state);
return $form['container'];
}
What's wrong with my code? Please help.
Comments
Your problem is probably here
Your problem is probably here:
You cannot generate form elements in ajax callbacks - and you are generating the entire form in your ajax callback. The only thing you should be doing in your ajax callback is returning the relevant part of the $form array. If you need to make changes to the form in your ajax call, that should happen in the form definition, checking $form_state['values'] to see if the form has been submitted or not.