My Form Submit Function Is Not Working - drupal

I'm developing a custom module for Drupal 6, that creates a simple form. My problem is that the submit function is not being called/processed!!! Here's my code:
function listgroups_menu(){
$items['user/%/groups-settings'] = array(
'title' => 'Groups Settings',
'page callback' => 'listgroups_groups_list',
'page arguments' => array(1),
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function listgroups_groups_list ($uid){
/*
* Couple lines here to access the DB & get the user's $groups.
*/
variable_set('listgroups_database_result', $groups );
$output = drupal_get_form('listgroups_settiongs_form');
return $output;
}
/**
* Form Builder
*/
function listgroups_settiongs_form(){
$groups = variable_get('database_result', array());
//Building the form
$form['display_option'] = array(
'#type' => 'checkbox',
'#title' => t('Show my group.'),
);
$form['groups_selection'] = array(
'#type' => 'radios',
'#title' => 'Please select your group',
'#options' => $groups,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return system_settings_form($form);
}
/**
* Submition
*/
function listgroups_settiongs_form_submit($form, &$form_state){
echo "<pre>I'm heeeeeeeeeeeeeeeeeeeeeerr!!!</pre>";
drupal_set_message('Your settings have been saved! YES!!!');
}
Now, the form rendering & the data retrival of the Db is just perfect. It's when I click the submit button, I get nothing at all!! Only the page refreshes & the messages doesn't appear!!
Any idea why?!!!!

use
return $form;
instead of
return system_settings_form($form);
and also
function xyz_form_submit($form, &$form_state){
//echo "<pre>I'm heeeeeeeeeeeeeeeeeeeeeerr!!!</pre>";
drupal_set_message('<pre>I\'m heeeeeeeeeeeeeeeeeeeeeerr!!!</pre>Your settings have been saved! YES!!!');
}
the problem was if you use system_setting_form then it start behaving as a system setting page that is generally used to store some information in database. So making it normal form you need to return only $form.

Include a submit handler and then assign it a function
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#submit' => array('my_module_function_submit'),
);
my_module_function_submit($form, $form_state){
.
.
.
.
.
}
Refer this link https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#submit_property

Related

I want to place 2 forms in a single custom page in Drupal 7

I am completely new to Drupal am trying to create a single page that contains 2 forms (that come from a custom module). I also want this page to be a dedicated file in my theme directory.
In the custom module called "vocabt" I have vocabt.module that contains 3 needed functions:
function vocabt_menu() {
$items['scores/admin'] = array(
'title' => 'Check Your Student\'s Scores',
'page callback' => 'drupal_get_form',
'page arguments' => array('vocabt_admin_login_form'),
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
$items['scores/student'] = array(
'title' => 'Check My Scores',
'page callback' => 'drupal_get_form',
'page arguments' => array('vocabt_student_login_form'),
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
}
function vocabt_admin_login_form($form, &$form_state) {
if(!empty($form_state['values'])) {
$values = $form_state['values'];
} else {
$values = array();
}
$schools = vocabt_get_school_options();
$form['school'] = array(
'#type' => 'select',
'#title' => 'Select Your School',
'#required' => TRUE,
'#options' => $schools,
'#default_value' => !empty($values['school']) ? $values['school'] : '',
'#empty_option' => 'Select',
);
$form['username'] = array(
'#type' => 'textfield',
'#title' => 'User Name',
'#required' => TRUE,
);
$form['password'] = array(
'#type' => 'password',
'#title' => 'Password',
'#required' => TRUE,
);
$form['actions'] = array();
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => 'Log In',
'#attributes' => array('class' => array('button blue')),
);
return $form;
}
function vocabt_student_login_form($form, &$form_state) {
if(!empty($form_state['values'])) {
$values = $form_state['values'];
} elseif(isset($_GET['id']) && $info = vocabt_decode_id($_GET['id'])) {
$values = $info;
} else {
$values = array();
}
$form['left'] = array(
'#prefix' => '<div class="lookup">',
'#suffix' => '</div>',
);
$form['right'] = array(
'#prefix' => '<div class="results">',
'#suffix' => '</div>',
);
$schools = vocabt_get_school_options();
$form['left']['school'] = array(
'#type' => 'select',
'#title' => 'Select Your School',
'#required' => TRUE,
'#options' => $schools,
'#default_value' => !empty($values['school']) ? $values['school'] : '',
'#empty_value' => 'Select',
);
$form['left']['id'] = array(
'#type' => 'textfield',
'#title' => 'Enter your student ID',
'#required' => TRUE,
'#default_value' => !empty($values['id']) ? $values['id'] : '',
);
$form['left']['submit'] = array(
'#type' => 'submit',
'#value' => 'Check',
'#attributes' => array('class' => array('button blue')),
);
if(!empty($form_state['storage']['error'])) {
$results = '<div class="blackbox">';
$results .= '<div style="padding: 20px 0px; text-align: center;">'.$form_state['storage']['error'].'</div>';
$results .= '</div>';
} elseif(empty($values['id'])) {
$results = '<div class="blackbox">';
$results .= '<div class="instructions">Your score will appear here once you’ve entered your information on the left.</div>';
$results .= '</div>';
} else {
$results = vocabt_get_student_results($values['school'], $values['id']);
}
$form['right']['results'] = array('#markup' => $results);
return $form;
}
My question is I want to hit a URL such as "/scores/admin" that NOT ONLY contains the form within the function of "vocabt_admin_login_form()" but ALSO contains the form within the above function "vocabt_student_login_form()" ....how can I do this?
I also want both forms to appear in page that uses a dedicated PHP file (I can already create custom pages using dedicated PHP files via creating a new page in the CMS), but I do not know how to tie this dedicated PHP file with the 2 forms above. Please let me know! Thank you
It's not clear what you mean by "Dedicated PHP files". In Drupal, you define menu router items using hook_menu(), and then return their data in the specific page callback function. This function can be in any .module file or any included file in normal PHP scope.
Add a new item in your hook_menu() implementation like this:
$items['scores/check'] = array(
'title' => 'Check Scores',
'page callback' => 'vocabt_scores_page', // Note this is the PAGE CALLBACK!
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
)
Now, lets create the page callback function, vocabt_scores_page, to return a render array so Drupal will build both forms in this page - not just the specified one.
function vocabt_scores_page() {
$output = array();
$output['admin'] = drupal_get_form('vocabt_admin_login_form');
$output['student'] = drupal_get_form('vocabt_student_login_form');
return $output;
}
Make sure you clear your site's caches before testing the code. Once saved, go to scores/check page and you will see both forms in one page!
You can move your page callback functions to a different file from the .module file. To do so, add 'files' => 'vocabt.pages.inc' to the menu item and move the functions to a new file named vocabt.pages.inc. Drupal will assume the file is in same folder as the .modulefile. This file name can be anything but we mostly use module.pages.inc, module.admin.inc, likewise.
You could call a template page using the hook_theme function. You could place your html contents in this page.And just call the theme('page_name', $link);.Place your newly created theme file in your corresponding module.
function modulename_theme() {
return array(
'page_name' => array(
'template' => 'page_name',
'arguments' => array('link' => NULL),
),
);
}
You could find more information here
http://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_theme/7
Hope this helps you... :)

drupal field widget not saving submitted data

I'm trying to create a custom widget but when I submit, Drupal doesn't seem to save any data. When using hook_field_attach_submit() to display what data I've pasted, it is listed as null.
Strangely, if i change the #type to be a single textfield instead of a fieldset it will save only the first character of the string that has been entered.
This seems like a validation issue, but I'm not sure how to hook into it or to debug the problem. Where can I go from here?
<?php
function guide_field_widget_info(){
dpm("guide_field_widget_info");
return array(
'guide_text_textfield' => array(
'label' => t('test Text field'),
'field types' => array('text'),
'settings' => array('size' => 60),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_CUSTOM,
'default value' => FIELD_BEHAVIOR_DEFAULT,
),
)
);
}
function guide_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$field_name = $instance['field_name'];
$required = $element['#required'];
$item =& $items[$delta];
$element += array(
'#type' => 'fieldset',
'#title' => t('helloooooooo'),
);
$required = $element['#required'];
$item =& $items[$delta];
$element['nametest'] = array(
'#title' => t('Name'),
'#type' => 'textfield',
'#required' => $required,
// use #default_value to prepopulate the element
// with the current saved value
'#default_value' => isset($item['nametest']) ? $item['nametest'] : '',
);
$element['checkme'] = array(
'#title' => t('Check this box or dont'),
'#type' => 'checkbox',
'#default_value' => isset($item['checkme']) ? $item['checkme'] : '',
);
//When changing the above code to have a single field, $value is no longer null but will display the first character of the string. I've pasted the code I used to test beloe
/*
$element+= array(
'#title' => t('Name'),
'#type' => 'textfield',
'#default_value' => isset($item['nametest']) ? $item['nametest'] : '',
);
*/
return $element;
}
//hooking this here is required given that after submit, the value is a multidimensional array, whereas the expected value of text is, well, text :-)
function guide_field_attach_submit($entity_type, $entity, $form, &$form_state){
dpm($form,"guide_field_attach_submit data"); //shows $form[field_test_field][und][0] [value] as being null
}
hook_field_is_empty is mandatory and has to be implement like following:
/**
* Implements hook_field_is_empty().
*/
function MODULENAME_field_is_empty($item, $field) {
if ($field['type'] == 'FIELDTYPE') {
if (empty($item[$field['type']]['YourField']) ) {
return (TRUE);
}
}
return (FALSE);
}

Drupal 7, how to dynamically populate a field based on the summation of two other fields?

I have two fields
$form["field_num_males"]
$form["field_num_females"]
I need to dynamically populate the field $form["field_gender_total"] with the summation of them, before the submission (AJAX).
How can this be done with Drupal 7?
Thanks!
Yes it can be done with Ajax. The following code will automatic update the total when the text field is updated:
function gender_total_menu()
{
$items = array();
$items['test'] = array(
'title' => Gender total,
'page callback' => 'drupal_get_form',
'page arguments' => array('gender_total_form'),
'access callback' => array(TRUE),
'type' => MENU_CALLBACK,
);
return $items;
}
function gender_total_form($form, &$form_state)
{
$total = 0;
if (array_key_exists('values', $form_state) &&
array_key_exists('field_num_males', $form_state['values']) &&
array_key_exists('field_num_females', $form_state['values'])
) {
$total = $form_state['values']['field_num_males'] + $form_state['values']['field_num_females'];
}
$form = array();
$form["field_num_males"] = array(
'#type' => 'textfield',
'#title' => t("Number of males"),
'#default_value' => 0,
'#ajax' => array(
'callback' => 'ajax_update_callback',
'wrapper' => 'wrapper',
),
);
$form["field_num_females"] = array(
'#type' => 'textfield',
'#title' => t("Number of females"),
'#default_value' => 0,
'#ajax' => array(
'callback' => 'ajax_update_callback',
'wrapper' => 'wrapper',
),
);
$form['total'] = array(
'#markup' => '<p> Total: ' . $total . '</p>',
'#prefix' => '<div id="wrapper">',
'#suffix' => '</div>',
);
return $form;
}
function ajax_update_callback($form, $form_state)
{
return $form['total'];
}
Try the Computed Field module
Computed Field is a very powerful CCK field module that lets you add a
custom "computed fields" to your content types. These computed fields
are populated with values that you define via PHP code. You may draw
on anything available to Drupal, including other fields...

Warning: Missing argument 2 for customvishal_form() in Drupal 7 :

I have made a custom module and it works fine till I start working on my custom theme.
Once I move over to my custom theme I get this error
Warning: Missing argument 2 for customvishal_form(), called in
/home/vishal/Dropbox/sites/new/includes/theme.inc on line 1029 and
defined in customvishal_form() (line 441 of
/home/vishal/Dropbox/sites/new/sites/all/modules/customvishal/customvishal.module).
You can see the error at : http://www.iamvishal.com/dev/about-us
I don't think anything is wrong with my code :
/**
* A simple form.
*/
function customvishal_form($form, &$form_submit) {
$form['customvishalactivate'] = array(
'#title' => t('Activate Preference'),
'#type' => 'radios',
'#options' => array('1' => t('Yes'), '0' => t('No')),
'#required' => TRUE,
);
return $form;
}
Its called from
function customvishal_pref($arg1)
{
// Here we willl make the form and save the data so when cron
// runs we will check the users preference
$build = array(
'header_text' => array(
'#type' => 'markup',
'#markup' => '<p>' . t('This page is where you add your preferences. Based on your
entered choices we will send you alerts ') . '</p>',
),
'example_form' => drupal_get_form('customvishal_form'),
);
return $build;
}
What might be causing this problem ?
Cheers,
Vishal
I had the same issue
I called hook_form like this:
/**
* Implements of hook_menu().
*/
function skupina_menu() {
$items = array();
$items['admin/config/people/skupina'] = array(
'title' => 'Skupiny odborníkov',
'description' => 'Prehľady návštev odborníkov',
'page callback' => 'drupal_get_form',
'page arguments' => array('skupina_statistics'),
'access arguments' => array('view statistics'),
'file' => 'admin.inc',
'file path' => drupal_get_path('module', 'skupina'),
'weight' => 1,
);
return $items;
}
and then
/**
* Prehlad navstev odbornikov - page
*/
function skupina_statistics($form, &$form_state) {
$form = array();
$form['skupina_obdobie'] = array(
'#type' => 'select',
'#title' => t('Zmeniť zobrazenie'),
'#options' => skupina_get_zobrazenia(),
'#description' => t('Zmení filtrovanie dát podľa zvolenej možnosti.'),
'#required' => FALSE,
);
return $form;
}
my problem was, that the function didn't have the "_form" in its name so its produce those warnings.
So the function must be called "skupina_statistics_form in my case
when the form function is called the only parameter that is sent to it is the form variable. Since your second function parameter doesn't have a default value it obviously produces a warning.
If you never use it in the function code you might consider removing it or providing a default value.
e.g.:
function customvishal_form($form, &$form_submit = NULL)
or you might consider passing an additional parameter. You can do this like so:
drupal_get_form('customvishal_form', $some_your_parameter);
I think I know the answer. I had the exact same problem.
Is your module named exactly as your custom theme? Mine was and I changed my theme name and the error went away

Drupal - add form element on successful submission

In a Drupal custom module, I want to make $form['link_wrapper'] conditional on a successful submission of the form but this is not a very successful way of doing this. Can anyone suggest better approach.
function my_function_my_form($form_state){
//echo "-" . $form_state['post']['op'] ."-";
//die();
global $base_root;
$form = array();
$form ['query_type'] =array (
'#type' => 'radios',
'#title' => t('Select from available Queries'),
'#options' => array(
"e_commerce_orders" => t("Query1"),
"new_orders" => t("Query2"),
"cancelled_orders" => t("Query3")),
'#required' => TRUE,
);
// only show link when submitted
if($form_state['post']['op'] == 'Submit')
{
$form['link_wrapper'] = array(
'#prefix' => '<div>',
'#value' => l("Click to View file"),
'#suffix' => '</div><br><br>',
);
}
// add submit button
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'));
return $form;
}
Have you tried setting your condition in the validate hook?
Something like:
function my_function_my_form_validate($form_state){
//some condition is true
$form_state['something'] = TRUE;
}
http://api.drupal.org/api/function/hook_validate/6
This is rough. I can't remember the args for hook_validate

Resources