Beginning to toy with impressive form api in 4.7.
I found I needed to do some validation against a date element. I used the nice automatic item, i.e.:
$form['details']['date'] = array(
'#type' => 'date',
'#title' => t('Start date')
);
Then I needed to do some validation. First I wanted to check if the user had set any date at all, but since the date #value automatically defaults to today's date there's no immediately obvious way to distinguish between "not set" and "set for today". Also, no way to doublecheck that a date in the past hasn't been chosen. Other scenarios too..
Anyway, to cut to the chase - I hacked form.inc to pass the timestamp of the default date along with the other info. In the form.inc function expand_date I replaced the first few lines at 788:
// Default to current date
if (!isset($element['#value'])) {
$element['#value'] = array('day' => format_date(time(), 'custom', 'j'),
'month' => format_date(time(), 'custom', 'n'),
'year' => format_date(time(), 'custom', 'Y'));
}
with the following. Note that I've moved generation of date element values outside the conditional because, in the event of a form_error on another form element, we would lose the "today" info if user has already set a date.
$this_day = format_date(time(), 'custom', 'j');
$this_month = format_date(time(), 'custom', 'n');
$this_year = format_date(time(), 'custom', 'Y');
//question: mktime or gmmktime?
$today = gmmktime(0, 0, 0, $this_month, $this_day, $this_year);
$today = format_date($today, 'custom', 'U');
if (!isset($element['#value'])) {
$element['#value'] = array('day' => $this_day,
'month' => $this_month,
'year' => $this_year);
}
Then, at the end of the function, I added the following to generate the extra form item:
$parents[] = 'timestamp';
$element['timestamp'] = array(
'#type' => 'value',
'#value' => $today
);
Finally, in my own module's _validate (or _submit) function, I convert the submitted date thusly:
$formday = gmmktime(0, 0, 0, $edit['date']['month'], $edit['date']['day'], $edit['date']['year']);
$formday = format_date($formday, 'custom', 'U');
Which gives a timestamp of same format, with timezone synched to the original timestamp, etc.
Comments? Improvements? Worth a patch?