How can I modify field value after Drupal webform sumission - php

I'm trying to modify value of the submitted field with php (server-side) to be send by mail and written to db:
hook_form_alter(&$form, &$form_state, $form_id)
changing of value looks easy, but nothing happens after I change it. Hook works.

hook_form_alter only manipulates the form before it gets rendered:
Perform alterations before a form is
rendered.
Have a look at this API comment, where someone gives a nice example of how to do something after the form has been submitted. There is also a _submit($form, &$form_state) action (the given post from the link points that out) that you need to trigger. You can do all your needed altering there. A little bit more description can be found in the examples from the API

First add a submit action in hook_form_alter and second alter the form_state value in form submission.
function custom_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'your-webform-id') {
$form['#submit'][] = 'xyz_form_submit';
}
}
function xyz_form_submit($form, &$form_state) {
// here you can edit $form_state value before final submit
}

Related

Add custom code into Drupal 7 webform

I'm looking to add a custom captcha input into a Drupal Webform. The captcha I wish to use isn't available as a plugin so I want to hook the form as it is output and then add my captcha presentation script.
I already know how to catch a form with <theme>_form_alter(). What I'm not so sure of is how I can inject elements into the form at a certain point. If anyone knows of how to achieve this it would be useful.
You could add your script with a preventDefault on the form submit. Do your client-side validation then submit the form. Adding your javascript would be as follows:
/**
* Implements hook_form_alter().
*/
function MODULENAME_form_alter(&$form, &$form_state, $form_id) {
dpm($form_id);
if ($form_id == 'YOUR_FORM_ID_HERE') { // Example Form ID: webform_client_form_23
$form['#attached']['js'][] = drupal_get_path('module', 'MODULENAME') . 'path/to/example_file.js';
// OR
drupal_add_js(drupal_get_path('module', 'MODULENAME') . 'path/to/example_file.js');
}
}
You can do it with ease by overriding the default form creation. Create a file in your template folder for your webform say its webform-form-22.tpl.php..
Use the $form variable which is available by default. Place the form elements from the $form variable and place your custom captcha according to your requirement anywhere in the form, and validate in the hook custom_module_webform_submission_insert..

Drupal 7 - how to add another button to a webform?

I was wondering, does anyone know how can I add another button to a webform in Drupal 7 in addition to the submit button? I tried looking through Google as well as asking this question at the Drupal forums, with no luck. Could anyone please help?
Webform is a drupal form and you can use hook_form_alter: Lets say you have a custom module called mymodule and your webform id is 'webform_client_form_XXXXX'. Then use the sample code below.
/**
* Implements hook_form_alter.
*/
function mymodule_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'webform_client_form_XXXXX':
$form['actions']['custom_submit'] = array(
'#type' => 'submit',
'#value' => t('Custom Button'),
'#submit' => array('mymodule_custom_submit'),
);
$form['#validate'][] = 'mymodule_custom_validate';
break;
}
}
/**
* Custom validation handler for your webform.
*/
function mymodule_custom_validate($form, &$form_state) {
// your validation code
}
/**
* Custom submit handler for your form
*/
function mymodule_custom_submit($form, &$form_state) {
// your submit handler code
}
UPDATE:
You would have to substitute instances of mymodule in the above code with yoru modules's name. Also i have modified the hook_form_alter a little. I suggest you move your submit button under $form['actions'], so it would appear next to the current submit button.
To Clear Cache:
visit /admin/config/development/performance and click on Clear all cache button.
To find the weform form-id:
This tutorial on net discuss about how to find the form id. Please note that the id used in the form tag will have '-' (hyphen), that has to be replaced with '_'(underscore) to get the form-id.
Another method is to print the $form_id variable in hook_form_alter ( the function which we have defined above ) and visit the webform page. If there are no other forms on that page the printed id would be the one that correspond to webform. If you enable devel module then it is easier to display debugging information using excellent krumo library.
/**
* Implements hook_form_alter.
*/
function mymodule_form_alter(&$form, &$form_state, $form_id) {
// Below function would print out the form id in message area if devel module
// is enabled on the site.
dpm($form_id);
switch ($form_id) {
remove dpm( $form_id ); afterwards.
Knowing the webform id is easier than the methods described above. You only need to go to: content->webforms and place the mouse cursor over the edit option belonging to the webform you want the id.
Down the navigator screen the original path in the format node/id will appear. That node number is the XXX in webform_client_form_XXX.
You can use type Markup in webform. Theme the markup as it look a like the button same as your submit button.
and write JS for that. For e.g, you want to give reset button then onclick event should empty the values in those fields. Try this.
first create markup in webform for button then use java script for that when click on this it process request based on your requirement.

Drupal webform, how can i make a form that triggers a php script with GET arguments?

I want to create a form in Drupal 7.
User will select appropriate listbox, radio button options.
When user clicks Submit button, form will post related values to another php file with GET arguments.
Default submit button shows a confirmation page for email sending/registration. I couldn't make the form to post arguments to a php file by changing submit behaviour of the form.
For two days I tried Webform module to do this.
I would be happy if you can recommend me some way to make this? Examples, tryouts, modules, codes, etc
There are 'better' ways but this will do the trick in a custom module:
function mymodule_form_alter(&$form, &$form_state, $form_id) {
$nid = 1; // Or whatever the node ID of your webform is
if ($form_id == 'webform_client_form_' . $nid) {
$form['#action'] = 'my-script.php';
$form['#method'] = 'get';
}
}
Bear in mind of course that the original webform submission functions will not run so your webform data won't be saved to the database.

What hook to listen to a form submit?

I'm writing a module that acts on another module. The other module's submit form is at admin/settings/image-toolkit. When its form is submitted, my module needs to respond to that event.
What hook do I need to listen for and how do I know the name of the form?
I'm not even sure where to print dsm in this case to get more information about this form. Is there something like hook_nodeapi but for forms that I could give me more info about the form?
All forms come with a $form[#submit] property that describes what functions are run when the form submits. The default is formname_submit, of course, but you just need to add new ones to that array.
So, you should use hook_form_alter and add another item to the $form['#submit'] array.
You can get the form id easily using the Devel module, or by looking for in the HTML of the pages themselves. (Hyphens should be translated to underscores if you take the latter route)
I get system_image_toolkit_settings for that form on my installations, but that might be dependent on which image library you're using (I use GD).
Though, I'll admit I'm scratching my head a bit about what submit handlers you're wanting to add to that one ;p
Edit:
Some sample code in reply to OP's comment:
What you're basically looking for is this: (from http://drupal.org/node/144132)
function my_module_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'my_form') {
$form['#submit'][] = 'my_additional_submit_handler';
}
}
Of course, you'll need to follow that up with function my_additional_submit_handler in your custom module for anything to happen.

hook_form_FORM_ID_alter not called after 'Submit' or 'Preview' is clicked

In a custom module, I have the following (to add javascript to a particular form):
function mymodule_form_mynode_node_form_alter(&$form, $form_state) {
drupal_add_js(drupal_get_path('module', 'mymodule') . '/js/mymodule.js', 'module');
}
This function does get called the form is first loaded (i.e. browse to http://myserver.com/node/add/mynode) however this php function does not get called when the same form is reloaded after the form has been invalidated (i.e. missed a required field after clicking 'Submit' or 'Preview').
What do I need to do to have the javascript file added after 'Submit' or 'Preview' is clicked?
Thanks,
John
To solve your problem, you'll need to create a theme function for the form where you render the form and add the js file.
You form alter is only called when the form is build. As forms are cached between request, the same and already build form is used on submit and preview.
You need to unsure that your code is called each time the form is rendered. As pointed out by googletorp, one way to do it is to use a custom form rendering function. You can also do it by attaching a post or pre-rendering function to your form. Actually, the custom, pre or post rendering function doesn't need to be for the form itself and can be used on any form element. Preferably the one which behavior is altered by the JavaScript.

Categories