I am currently work with a drupal's inbuilt form which is coming from it's system function. Now I added a extra field with value in the form with Drupal's custommodule_form_deal_node_form_alter(). It is working perfectly. But my problem is How to insert the value in the database with the default form's value. As the form's has its own default submit function. I know it may be done using custommodule_form_deal_node_form_alter_submit($form,$form_state) But dont know how to merge my value with the default form's submit function. Please help me.
2 Answers
In your alter hook you can create custom submit handlers.
Ex.
function custommodule_form_deal_node_form_alter() {
if (isset($form['actions']['submit'])) {
$form['actions']['submit']['#submit'][] = 'custom_form_submit';
}
}
then you implement the custom submit handlers.
function custom_form_submit(&$form, &$form_state) {
// $form_state will have the form value which you can insert into database.
}
-
I tried that. But my question is the default form is inserting its own value to table. like id, name, address etc. Now I want to update my barcode value(this field I altered in the form) against the id. for that how to get the id in the the altered submit function? If I get the id in
function custom_form_submit(&$form, &$form_state) {}
then I can update the database table. Commented Mar 10, 2016 at 5:55 -
Can you do print_r($form_state); and see. i think it should give you all the values.– mksCommented Mar 10, 2016 at 6:01
-
I tried this.
function custommodule_form_deal_node_form_alter_submit($form,$form_state){ $barcodeval=$form_state['values']['barcodeVal']; $sku=$form_state['values']['field_product']['und']['form']['sku']; $created=$form_state['values']['created'];; db_query("update commerce_product set barcode_val='".$barcodeval."' where sku='".$sku."' and created=".$created); }
. But database table is not updated with the barcode value. Am I doing it right? Is there any way using which I can use the insert command to insert the barcode value with form's default insert action insstead of update query? Commented Mar 10, 2016 at 9:29
For a node form you'll want to hook into the submit button, which is in the actions container:
$form['actions']['submit']['#submit'][] = 'mymodule_foo_submit';