Additional submit event handler for webform in Drupal 6 - php

Im using Drupal 6.19 and the webform module on my site.On a particular form,i want to do some additional processing with the values entered,so i created a module and implemented hook_form_alter, and added my own function called mymodule_webform_submit to the list of submit event handlers.
Suppose i have two fields in the form having field key values name and address respectively. How to print the values of those fields in mymodule_webform_submit function ?. Some example would be really appreciated. Below is my code so far.
<?php
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'webform_client_form_8') {
$form['#submit'][] = 'mymodule_webform_submit';
}
}
function mymodule_webform_submit(&$form,&$form_state) {
drupal_set_message($form_state['values']['name']);
}
?>
Please help
Thank You

You're missing the arguments to your submit function. Once those are added, your values should be in $form_state['values']. It should be something like:
function mymodule_webform_submit($form, &$form_state) {
print $form_state['values']['name'];
print $form_state['values']['address'];
}
But do whatever you want with the values in there. Just make sure you have the correct element keys. You could see what's available by putting var_export($form_state['values']); exit(); inside the function and submitting the form.

Related

Drupal multiple custom field required, pass validation if field is empty

I've build a custom field with several values. I've to make this field required. But I want to pass the validation if at least one field is filled and the last one is empty.
But my problem is Drupal warn me that the last (empty) field is required. I've thought that the hook_field_is_empty() solved the problem, but, even if return true, the form cannot be validated.
Many thanks for your help.
Implementation :
function MYMODULE_field_widget_form(...) {
$element['address']+=[
...
'#required' => $instance['required'],
];
...
}
function MYMODULE_field_is_empty($item, $field) {
if (empty($item['address']) && empty($item['other'])) {
return true ;
}
return false ;
}
To solve this problem, I've made my field not required (in the definition of the node's fields). Then, I added a callback in the #validate using the hook_form_alter(). In that callback, I test if at least one field is defined and call a form_set_error() if none is defined.
This makes sense because it's not the field itself that could know all the data of the node. But, something is a bit wrong because it's not possible to mark this field as required (e.g. asterisks).
<?php
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
if ($formid == ...) { // a specific case
array_unshift($form['#validate'], '_MYMODULE_validate_form') ;
}
}
function _MYMODULE_validate_form(&$form, &$form_state) {
if (empty($form_state['values']['field_geo'][LANGUAGE_NONE][0]['address'])) {
form_set_error('field_geo', t('You have to define at least one address.')) ;
}
}
?>

Custom callback on user_register_form Drupal 7

I have called a custom function on form submit of Drupal User Registration.
Below are the functions that i implemented
/**
* Implements hook_form_alter()
*/
function voen_registration_form_alter(&$form, &$form_state, $form_id) {
//dpm($form);
switch($form_id){
case 'user_register_form':
$form['account']['name']['#required'] = FALSE;
$form['account']['name']['#type'] = 'hidden';
array_unshift($form['#submit'],'voen_generate_username');
print_r($form);
break;
}
}
function voen_generate_username(&$form, &$form_state){
//drupal_set_message('Function Running');
die('Wokring');
}
Now, when the form is submitted i print_r the $form and in submit key i get the following result which is what i expect
[#submit] => Array
(
[0] => voen_generate_username
[1] => user_register_submit
)
But I don't know why my custom function is not executing. I tried with die() as well se drupal_set_message() and also tried to print_r() in custom function but no response. It is still giving me error of 'Please Enter Username' which is comming from user_register_submit function from user module.
Thanks
The submit function is not executed because the validation function is not passed.
Take a look at $form['#validate'], there is an "user_account_form_validate" function which will not pass if you not define at least a default value for name field.
Anyway, if you're trying to allow registration only by email take a look at this module:
https://www.drupal.org/project/email_registration

Passing form value from one form/page to another - Laravel

Here is an explanation of my question.
I have 2 forms:
add & add1
2 pages:
add.php & add1.php
and 2 functions:
function post_add & function post_add1
when the user has fill and submit the form add, function post_add automatically redirect to user to add1.
I want to pass value (after the user fill it and submit it) from form add
"echo Form::text('name', Input::get('name'));"
to form add1, because you need to type the same thing on next form.
Thanks in advance!
Just use Session::flash();
Like so...
function post_add(){
//blah blah your code.
$passvalues = Input::all();
Session::flash('values', $passvalues);
Redirect::to('/add1');
}
function post_add1(){
//Now you have access to that input...
$oldinput = Session::get('values');
//do whatever you need to with the old/passed input
}
Or you could just do a with()....
function post_add(){
//blah blah your code.
$passvalues = Input::all();
Redirect::to('/add1')->with('values', $passvalues);
}

Using Hook_form_alter on webform submitted values

Drupal 7. Webforms 3.x.
I am trying to modify a webform component value on submit. I made a custom module called 'mos' and added this code to it.
function mos_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'webform_client_form_43') {
dsm($form['#node']->{'webform'}['components']['1']);
$form['#submit'][] = 'mos_contact_us_submit';
}
}
function mos_contact_us_submit($form, &$form_state) {
$form['#node']->{'webform'}['components']['1'] = 'working#mos.com';
}
However when I look at the results in the database the regular, non-overridden value is stored. Can you help let me know what I am doing wrong?
Eventually I want to take the input value and output an email address based on what was provided (for example. 24 turns into bob#somewhere.com) But I think I can figure this part out myself.
You should to place your submit first.
array_unshift(
$form['actions']['submit']['#submit'],
'mos_contact_us_submit'
);
However, if you want to change some variables in form_state, you should to using custom _valadate function.
I got it! BIG Thanks to #dobeerman for pointing me in the right direction. Here is the code that ended up working:
function mos_form_alter(&$form, &$form_state, $form_id) {
if ('webform_client_form_43' == $form_id) {
//dsm($form);
$form['#validate'][] = 'mos_check_email';
}
}
function mos_check_email(&$form, &$form_state, $form_id) {
$emailVal = $form_state['values']['submitted']['to'];
switch($emailVal) {
case 1: $emailVal = 'email#test.com'; break;
case 2: $emailVal = 'email2#test.com'; break;
case 3: $emailVal = 'email3#test.com'; break;
......
}
$form_state['values']['submitted']['to']=$emailVal;
//dpm($form_state);
}
This way I can keep email address private, but still pass variables to the form with _GET. Kind of a weird situation... but we are trying to keep some existing code intact, so it seemed like the best route.
I accidentally messed up my account creation, so I can't give you the credit dobeerman but I emailed the admins and hopefully I will get it straightened out to get you some rep!

Drupal - CCK field - make required

I've installed the following module - http://drupal.org/project/og_reg_keys This module adds an additional field to your Organic Group Node types, to allow auser to specify a registration key for users to use to join the group.
The problem is that field is not required to be entered by the user.How can one make this field a required field ?
I found the below code, which makes the CCK field mandatory for users of a specific role, but being a non PHP person I have no idea how to change this to:
Make the Group registration key a required field (not sure what the $form element would be called or where to find this)
To remove the section on the code where it applies to users of a specific role, so that it always applies.
Code:
function mymodule_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'profile_node_form':
global $user;
if(in_array('targetrole', $user->roles)) {
$form['field_profile_pic'][0]['#required'] = 'TRUE';
$form['#field_info']['field_profile_pic']['required'] = '1';
break
Any help would be greatly appreciated. Sorry for the code being so messy, I couldn't seem to paster it correctly, it kept getting cut off.
This should make it required for all users:
function mymodule_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'profile_node_form':
$form['field_profile_pic'][0]['#required'] = 'TRUE';
$form['#field_info']['field_profile_pic']['required'] = '1';
break ;
}
}

Categories