1

If I want a custom submit handler, the usual answer is to add the function call to $form['#submit'], and if need be, $form['actions']['submit']['#submit']. The standard warning is do something like

$form['#submit'][]='my_handler';

instead of

$form['#submit']='my_handler';

because you generally want the form submission to still go through the core form submission handlers.

I have a case where I need to completely manage the form submission process and don't ever want the form to be passed to the core submission handlers. To test that I could do this, I have:

global $user;   
module_load_include('inc', 'node', 'node.pages');
$node_form = new stdClass;
$node_form->type = 'my_content_type';
$node_form->uid = $user->uid;
$node_form->name = (isset($user->name) ? $user->name : '');
$form = drupal_get_form('my_content_type_node_form', $node_form);
$form['#submit']='';
$form['actions']['submit']['#submit']='';
$render($form);

I would have thought that since there are no submission handlers, the form shouldn't submit, or even if the page reloaded due to the action attribute in the html form element, that the content wouldn't be added to the database.

But it is.

So how can I keep Drupal from processing a submission through the normal core submission handlers, but still process through custom handlers? (I already know how to write custom form submission handlers, so that's not part of this question.)

  • Is the function callback my_content_type_node_form something that you defined? Can you post the code for that function. I wonder if it may be a better idea to implement hook_form_alter() once the node form has been built by drupal, and modify the submit handlers in the alter hook, rather than using drupal_get_form to build the form from scratch. – argiepiano Oct 29 '15 at 18:34
  • This code is slightly generalized. But using this generalized code, I would have a content type with the machine name my_content_type. Therefore, the standard form to submit new content of that content type is my_content_type_node_form. That's not something I defined. That's standard core. So there shouldn't be any difference between: function hook_form_alter(&$form, &$form_state, $form_id) { if($form_id=="my_content_type_node_form"){...} and $form = drupal_get_form('my_content_type_node_form', $node_form); – user53772 Oct 29 '15 at 19:35
  • "Shouldn't" is a great word in our line of work :) Try the hook_form_alter() method (I would've suggested the same thing) if only to humour us, because it should work. If not, then we can start looking for the real problem – Clive Oct 29 '15 at 19:44
  • Oh, submit handlers are always an array BTW, so $form['#submit']=''; would be $form['#submit']=array();. Probably not causing the problem, but worth ruling out. If i had to put money on what the problem is, I'd say that you're removing the submit handlers too late in the process, i.e. after drupal_get_form has already cached all of that info – Clive Oct 29 '15 at 19:47
  • Now that's interesting. I threw a function my_theme_form_alter(&$form, &$form_state, $form_id) { if($form_id=="my_content_type_node_form"){ $form['#submit']=array(); $form['actions']['submit']['#submit']=array(); } } and on submit, no data hit the database. Thanks! Any idea why using drupal_get_form and setting the #submits to array() doesn't accomplish the same thing? – user53772 Oct 29 '15 at 20:03
0

Since I suggested this in my comment (the first in the thread) I'll rephrase as an answer so that you can mark it as correct.

Rather than using drupal_get_form() to build the node edit form (which just doesn't work the way you tried to do it, as there is no such callback my_content_type_node_form as far as I know) and then modifying the $form array, my suggestion is to:

  • Let Drupal build your node edit form (you can do this by calling the appropriate URL /node/8/edit or /node/add/[type])
  • Then use the widely documented way of altering a form by implementing hook_form_alter.

EDIT: Digging a bit more I did find a hook_form function callback for nodes, but it's standard for all nodes - it's implemented in the node.module as node_content_form(). The hook is invoked in node_form() but as far as I can tell (I did some trials) it's the same for all Content Types regardless of their machine name. I think this may be where the confusion started.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.