In my node form I have a 'date' form element. Its value is converted to a unix timestamp in hook_submit and saved to the database. In hook_view the date is displayed with format_date(). Prior to editing the node the timestamp is converted to an array in hook_prepare.
Up to this point everything is fine.
But if I want to preview the node I get an Unsupported operand types error, because my $node->mydate field is still an array containing day, month and year as separate elements which format_date() in my hook_view does not like.
Since node previews are generated during form building via '#after_build' I don't see any chance (hook, element property) to perform the date conversion to a timestamp before the preview is generated. How could this be done?
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | node.validate.patch | 1.39 KB | tenrapid |
Comments
Comment #1
tenrapid commentedSince nobody could answer this support request I make this into a bug. The thing is that I see no straightforward way to alter a node form value before the node preview is generated.
Normally one could alter a form value during form validation with form_set_value() but this is not possible in hook_validate for two reasons:
- the form array is not passed to hook_validate
- node_form_add_preview() gets only a copy of $form_values, calls then drupal_validate_form() and passes the $form_values copy to node_preview() so changes to global $form_values with form_set_value() have no effect to the preview
Or did I miss something?
Comment #2
tenrapid commentedWith this patch hook_validate no longer stays behind every other regular form validation callback. It gets the form array passed and you can use form_set_value() in it.
Comment #3
tenrapid commentedstatus ...
Comment #4
tenrapid commentedThe following are some module code extracts to depict what is possible with this patch.
Note that this can already be done with every regular form but with node forms you can't.
Imagine a node that contains a date field. Its value is stored as a unix timestamp in the database. This is how the form hook looks like:
To convert the timestamp into the array that is needed for the date form element hook_prepare is used:
This is how hook_view looks like:
And here hook_validate which is affected by the patch. The date is validated and if it's valid it is converted to a timestamp.
With this $node->mydate is already a timestamp at preview and submit time.
I think this is as straighforward as possible. Plus you get consistent behaviour with all forms in Drupal.
Comment #5
tenrapid commentedThe above is now possible in 4.7 because this patch http://drupal.org/node/59935 got in.