By thx538 on
I'm new to Drupal and developing a simple module with a custom form (with a few custom fields, stored in a custom db table) associated to a content type.
function mymodule_form(&$node) {
$form['firstname'] = array(
'#type' => 'textfield',
'#title' => 'First name',
'#default_value' => isset($node->firstname) ? $node->firstname : '',
'#required' => FALSE,
);
$form['lastname'] = array(
'#type' => 'textfield',
'#title' => 'Last name',
'#default_value' => isset($node->lastname) ? $node->lastname : '',
'#required' => FALSE,
);
$form['title'] = array(
'#type' => 'value',
'#default_value' => isset($node->title) ? $node->title : '',
);
return $form;
}
Upon creation or modification of the node, I would like to force the node title with let's say something like this :
$node->title = $node->firstname . ' ' . $node->lastname;
... and have tried to place this code in :
- mymodule_validate
- mymodule_submit
- both mymodule_insert and mymodul_validate
but none of these seems to work, so I'm probably missing something.
Thanks for your support.
Comments
...
In D6 you can no longer change stuff in this hook.
This hook is gone in D6
(...because it was meant to do form massaging, which FAPI itself can do --this hook wasn't even called when node was saved through node_save().)
These are invoked after the node title has been saved already.
You can either use hook_nodeapi($op=='presave') or write a FAPI #submit handler.
(Or, you should have been able to use $form[#post] to construct the value, if it weren't for some FAPI bug.)
Thank you
I finally ended up with a $form['#submit'] callback and a form_set_value within the callback, which seemed more elegant that the hook_nodeapi.
submit button still fires hook_submit
Oops! meant to comment on http://drupal.org/node/243030. Sorry for dup.
I've been testing a simple form with a submit button which fires hook_submit for me. So lets be clear, form hook_submit may be depricated but button hook_submit is still OK, Yes?
However, I can't get redirect to work by setting form_state['redirect']. Is that related?
Is it OK to use drupal_goto instead? (it seems to work in my simple test)
But surely form_state['redirect'] should work?
...
form's hook_submit isn't deprecated. We were talking about node's hook_submit.
That's because you're modifying a _copy_ of $form_satate. Pass it by reference.
Not really, because it aborts the script. So if someone installs a submit handler after yours, it won't execute. Or if your form is submitted via drupal_execute(), control won't return to the executing code.
now I'm clear
Thank you so much mooffie, now I'm clear AND you solved my problem.
That little & sign made all the difference.
For any other newbies out there, my working form hook_submit with pass by reference is:-
(note to self, brush up on PHP syntax!)
Thanks again mooffie.