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

mooffie’s picture

... and have tried to place this code in :
mymodule_validate

In D6 you can no longer change stuff in this hook.

mymodule_submit

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().)

both mymodule_insert and mymodul_validate

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.)

thx538’s picture

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.

stevemasters@cherryhill.co.uk’s picture

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?

function test_node_info() {
  return array('my_form' => array('name' => t('test'),'module' => 'test','description' => 'test',));
}
function test_perm() {
  return array('test');
}
function test_menu() {
	$items = array(); 
    $items['testform'] = array(
		'title' => t('test'),
      	'page callback' => 'drupal_get_form',
      	'page arguments' => array('testform'),
    	'access arguments' => array('test'),
    	'type' => MENU_CALLBACK,
	);
    $items['redirect'] = array(
		'title' => t('test redirect'),
		'page callback' => 'redirectcallback', 
    	'access arguments' => array('test'),
      	'type' => MENU_CALLBACK, 
	);
    return $items;
}
function testform() {
    $form = array();   
    $form['submit'] = array(
		'#type' => 'submit', 
		'#value' => t('test redirect'),
	);
	return $form;
}
function redirectcallback() {
	return 'redirected';
}
function testform_submit($form, $form_state) {
    drupal_set_message('goto redirect works');
    drupal_goto('redirect');
//    drupal_set_message('form_state redirect doesn't');
//    $form_state['redirect'] = 'redirect';
}
mooffie’s picture

So lets be clear, form hook_submit may be depricated but button hook_submit is still OK, Yes?

form's hook_submit isn't deprecated. We were talking about node's hook_submit.

can't get redirect to work by setting form_state['redirect'].
...
function testform_submit($form, $form_state) {
...
$form_state['redirect'] = 'redirect';

That's because you're modifying a _copy_ of $form_satate. Pass it by reference.

Is it OK to use drupal_goto instead?

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.

stevemasters@cherryhill.co.uk’s picture

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:-

function testform_submit($form, &$form_state) {
    $form_state['redirect'] = 'redirect';
}

(note to self, brush up on PHP syntax!)

Thanks again mooffie.