Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Code sample #7:
This adds the submit function, also referred to as a submit handler.
<?php
/**
* Implements hook_menu().
*/
function my_module_menu() {
$items = array();
$items['my_module/form'] = array(
'title' => t('My form'),
'page callback' => 'drupal_get_form',
'page arguments' => array('my_module_my_form'),
'access arguments' => array('access content'),
'description' => t('My form'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Returns the render array for the form.
*/
function my_module_my_form($form, &$form_state) {
$form['name'] = array(
'#type' => 'fieldset',
'#title' => t('Name'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['name']['first'] = array(
'#type' => 'textfield',
'#title' => t('First name'),
'#required' => TRUE,
'#default_value' => "First name",
'#description' => "Please enter your first name.",
'#size' => 20,
'#maxlength' => 20,
);
$form['name']['last'] = array(
'#type' => 'textfield',
'#title' => t('Last name'),
'#required' => TRUE,
);
$form['year_of_birth'] = array(
'#type' => 'textfield',
'#title' => t('Year of birth'),
'#description' => 'Format is "YYYY"',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/**
* Validates the form.
*/
function my_module_my_form_validate($form, &$form_state) {
$year_of_birth = $form_state['values']['year_of_birth'];
if ($year_of_birth && ($year_of_birth < 1900 || $year_of_birth > 2000)) {
form_set_error('year_of_birth', t('Enter a year between 1900 and 2000.'));
}
}
/**
* Add a submit handler/function to the form.
*
* This will add a completion message to the screen when the
* form successfully processes
*/
function my_module_my_form_submit($form, &$form_state) {
drupal_set_message(t('The form has been submitted.'));
}
Comments
Calling a View with arguments from the form
It only requires a small change to be able to call a View with arguments from the form. For example, imagine you have a View which takes two arguments, First name and Last name. The form could call this View based on the values the user has selected, e.g.
drupalshrek
Need to specify submit handler?
Some documentation (http://drupal.org/node/222158) seems to say you would need to have a line in my_module_my_form
but it seems to work the same either way. Can someone explain? Is it only necessary when the form doesn't provide a submit button?
Specify submit handler only when there are more
xenophyle, that documentation says that it's necessary when you want to add additional submit handlers. The example specifies the default handler, but, you would add more handlers with the same syntax.
Actually, the reason they presented the example this way is that the example shows a form which does nothing but set var's on submit, which you don't even need a handler for (not as written, anyway). So the default handler is an "extra" handler in that case.
An Additional Submit Handler Would Look Like This
The default submit handler is always the name of the form function (e.g., my_module_my_form in the code example above) with "_submit" added. So, an additional submit handler is any submit handler function other than the default.
I've needed to add $form['
I've needed to add $form['#submit'][] = 'my_module_my_form_submit' to make this work.
Maybe its because I'm creating a CK2 module?
Finding Variables
Just to bridge the gap for people new to hook_form_alter. To find the appropriate variables for use in your submit handler function, insert dpm($form_state); inside your function, not in the main hook_form_alter in which you've declared the #submit attribute. The $form_state variable structure changes with each stage of the form. Now to see the variables you can work with in the Save stage, edit a form then submit, and the correct variables will be output on the next page.
form submit handler not working.........
Form submit handler not working
I have encountered this issue. The function my_module_my_form_submit doesn't seem to get called. Is there a way out.
Acer Aspire 5745
[i5 430M, 3GB, 320GB]
Ubuntu 12.04 (Precise Pangolin)
Drupal 6.15, 7.x
DigitalOcean, Go Daddy, Rackspace,
You have to add
You have to add $form['#submit'][] = 'my_module_my_form_submit'; to my_module_my_form(). That made it work for me.
Answer : Form submit handler not working
I had the same issue, the solution is that you need to enter valid values in email, pass etc while submitting the form.
I will start working...
Modifying Tab Headers
I used the example for drupal 7.x
I am not finding a way to modify the tab labels like you see on this page.
http://d7.drupalexamples.info/examples/form_example/tutorial
Any help would be much appreciated.
Steve
Drupal 7.x
Does it work for Drupal 7.x or what is the appropriate way for Drupal 7?
D7
It does work for D7!
Call function from .inc file
Thought I'd drop this on here for those looking to call a function from a .inc file. I needed this today and had to locate a couple of pieces of information around the forums, etc.
I needed to pass the textfield value to a function in my .inc file, so this is how I did it:
This is just the PoC for me, so it needs validation and direction on what to do once it returns, but this is the basis of the idea. (Also will make my titles more descriptive to the user, but you get the point)