Using the Form API and hook_form_alter(), how can I manually set (force) the values for each of Drupal 7's date field types using a valid Unix Timestamp?
When the form is finally submit the field_date types are always empty arrays. Below is the broken code I'm working from.
hook_form_alter(&$form, &$form_state, $form_id) {
// http://drupal.org/node/1455576 - Date
$form['field_date'][LANGUAGE_NONE][0]['value'] = time();
// http://drupal.org/node/1455576 - Date (ISO Format)
$form['field_date_iso_format'][LANGUAGE_NONE][0]['value'] = time();
// http://drupal.org/node/1455576 - Date (Unix Timestamp)
$form['field_date_unix_timestamp'][LANGUAGE_NONE][0]['value'] =time();
}
I should mention the fields widget is set to text and it's format is 'Y-m-d H:i:s'
I have also tried date('Y-m-d H:i:s', time())
instead of time()
.
Example dpm() output.
I should also note that as a workaround I can set the field if I modify it in hook_node_presave(). To do this I set $node->field_date['und'][0]['value'] = date('Y-m-d H:i:s', $my_unix_timestamp);
I would still like to understand how to do this using hook_form_alter instead.
$form['field_date'][LANGUAGE_NONE][0]['#default_value']['value']
? – Countzero Mar 30 '12 at 8:10