In 4.6 I had a code snippet containing a form, where after submission I defaulted the form items based on the previous form entries. (Amongst other things it contains a day of the month, and next time around it defaults to the next day... a very specific one-a-day simple event entry).
After upgrading to 4.7 and making the relevant changes, I found that after submission the updates were done and then the form was redirected back to itself. This has the unfortunate side effect of losing the contents of the $_POST[] variable so I could no longer default my entry to the next day. Hunting through the api, I've discovered that if I set $form['#redirect'] = FALSE, I can prevent the redirection, but I'm also finding that the form retains it's previous entries and not my new ones. Can anyone help me out, and tell me if there is a way of doing what I would like to do?
A cut down version of the snippet follows. Thanks.
<?php
if($_POST["op"]){
// After posting, I want to default day to the day after the submitted one
$day = $_POST['edit']['day'] + 1;
} else {
// First time in, default to today
$day = date('d');
}
drupal_set_message($day);
// I don't want to redirect after posting, as I want to set defaults
// based on previous entry
$form['#redirect'] = FALSE;
// create the form and set default as per above
for ($i = 1; $i <= 31; $i++) $days[$i] = $i;
$form['day'] = array('#type' => 'select', '#default_value' => $day, '#options' => $days);
$form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
print drupal_get_form('myform', $form);
return;
function myform_submit($form_id, $form){
// do update stuff
drupal_set_message(t('Update done'));
}
?>
Comments
Worked it out sorta
Well I've played around, after realising that drupal_get_form is also responsible for calling the myform_submit, etc, (obviously, duh) and sets all the '#value's which the submit uses... but then these seem to override the #default_values in the form creation.
So I changed the code from
to
I'm sure there must be a nicer, more efficient way, but at least this works!