Form submit handlers with additional arguments - drupal

For some requirement I need to pass additional information to form submit handler. In form api, while defining custom submit handler as
$additional_args = array();
$form['#submit'][] = 'my_submit_handler'
I expect to submit handler as
function my_submit_handler($form, &$form_state, $additional_args){

The submit handler is called by the drupal fapi, so you can't do something like that. Instead what you can do, is to add what you need, either to the $form, or to the $form_state. The usual approaches is to:
Added a field to the form, type value to store the value. Don't do this if you have the value in the form definition.
$form['store'] = array(
'#type' => 'value',
'#value' => $value
);
This will be available in $form_state['values']['store'].
Add the value to $form_state['storage'], done if you variables in your validation handle you want to transfer to your submit handler:
// Validation.
$form_state['storage']['value'] = $value;
...
// Submit
$value = $form_state['storage']['value'];
// Need to unset stored values when not used anymore.
unset($form_state['storage']['value']);

Drupal 7: Custom arguments are automatically propagated troug $form_state['build_info']['args']
This is said in http://api.drupal.org/api/drupal/includes!form.inc/function/drupal_get_form/7
Ex:
hook_form($form, &$form_state, $myAdditionnalArg) {...}
Then in
hook_form_submit($form, &$form_state) {
...
//$form_state['build_info']['args'] is an array containing at index 0 the value of argument $myAdditionnalArg
...

As reported in $form['#submit'] and $form['#validate'] and $form['#process'] no longer support custom parameters, the suggested way to pass parameters to a submission handler set as in the shown code is to use code similar to the following:
$form['#first_paramater'] = $value;
$form['#submit'][] = 'my_submit_handler';
The handler would retrieve the value as $form['#first_paramater'].
To notice that, instead of #first_paramater, the code can use a different string, but it must start with #.
Normally it's not necessary to set a submission handler like the code does, but there are some cases where it is necessary, like to alter a form created by another module, or to set a different submission handler for each of the submission buttons present in a form.
drupal_retrieve_form() saves the parameters passed to the form build handler in $form['#parameters'] which contains:
$form_id
$form_state
parameters passed to the form builder

Related

Within a Callback validator for a Symfony sub-form collection, how do I get the value of a different field?

Let's say I have a Callback validator attached to a field in a collection sub-form that looks like this:
$callback = new Callback([
'callback' => function($obj, $context) {
// Value of field to validate, no problem
$value = $context->getValue();
// Get the form, but gets my parent form
$form = $context->getRoot();
// Try and get other field value I need to validate current field,
// but gets "child "otherfield" does not exist" error.
$otherValue = $form->get('otherfield')->getData();
// continue validation logic...
}
]);
I need the value of another field from the specific form instance for the collection item, but I can't find a way to access the child form instance. How do I do this? I can access the list of forms for the collection by using the parent field that holds the collection, but I need the specific instance that this is validating.
It's Symfony version 3.3, but I can't find anything in a later version, either.

Drupal custom form twice on same page, i need the errors displayed in the one submitted and not on both

I've a custom module that builds a form with a couple of fields, so far so good.
In one of my pages, i print this form twice (different blocks), the form gets the same "form_id", so when i submit one of them and get an error, both of them get the error highlighted, and the fields populated.
I want that only the form i submit gets the errors, is there a way to do this?
Thanks!!
For anyone interested, to do this you need to use the hook_forms.
This hook only gets called when the form_id passed to a drupal_get_form doesn't exist, this is important, if you want to use this, make sure your calls use a non existing form_id, for example:
//Defining the form:
function mx_wtransnet_form_contacto($form, &$form_state, $block = null, $formType = null) {
}
I want to use this form multiple times and get different error handlers, instead of loading my form (mx_transnet_form_contacto), i'll call a non existing one:
$form = drupal_get_form("mx_wtransnet_form_contacto_invalid", "contacto-mini");
Then i create my hook:
function mx_wtransnet_forms($form_id, $args) {
$forms = array();
if (strpos($form_id, '_contacto_') !==false) {
$forms[$form_id] = array(
'callback' => 'mx_wtransnet_form_contacto',
);
}
return $forms;
}
This function will catch all my druapl_get_form calls that don't exist, so i can process/direct them, in my example, what i do is simply check that the form_id contains contacto and then set the callback for this form to the original function.
In this case better to create another form with different "form_id" but with the same submit handler.
Another case: when you output same form twice on the page it also may get JS errors because ID of form elements are the same.
In case you don't repeat the form code and its submit handler(DRY principle), I would recommend create a custom function that has the form array
function form_my_custom($form_id){
$form['my_first_field'] = array();
$form['my_second_field'] = array();
$form['#attributes']['id'] = $form_id;
$form['my_submit_button'] = array(
'#submit' => array('my_custom_form_submit')
);
return $form;
}
function my_block1_form(){
return my_custom_form('my_form_id_1');
}
function my_block2_form(){
return my_custom_form('my_form_id_2');
}
function my_custom_form_submit(&$form, &$form_state){
// your submit handler.
}

How to pass variable from form submit function to page callback function in Drupal 6

I have a Drupal form and a submit function that processes that form. I would like to pass data from the submit function to the page callback function that loads after the form was processed.
Well, 10 months later of Drupal development later, I believe the $_SESSION variable is the only way of doing this. Drupal doesn't have any special tools for this.
It's a bit "late" but I feel to propose a new solution as I just found this post. Submit and Callback functions share the same $form and $form_state variables. If you want to pass a variable from one to another you can do it this way:
function YOUFORM_submit($form, &$form_state) {
// Set the variable
$form_state['values']['NEW_VAR'] = NEW_VALUE;
}
function YOUFORM_callback($form, &$form_state) {
// Get the variable
$new_value = $form_state['values']['NEW_VAR'];
}
More informations here : $form_state keys

retrieve cck field for form_alter

I would like to retrieve a cck field "field_info" in my form alter to insert into another table when user is submitting. This doesn't seem to work.
//mymodule_form_alter() implemented
function mymodule_form_mysubmit{
$test = $form['field_info']['#value'];
//insert stuff code
}
Is there any mistake in the code?
You say module_form_alter() is implemented, but just to confirm, you need to have the following in it:
$form['#submit'][] = 'mymodule_form_mysubmit';
Assuming you do, to get the value of field_info, your submit function should look like:
function mymodule_form_mysubmit($form, &$form_state) {
$test = $form_state['values']['field_info'][0]['value'];
}
$form_state contains the current state of the form being submitted. CCK always assumes that there could be multiple values for a field, so it always puts things in an array (hence ['field_info'][0]).
I found the solution
$test = $form['field_info'][0]['#value'];

Drupal hook alter with hook validate

I have a CCK defined form called 'mytype_node_form'.
I create a module called form_overrides
I create a function called form_overrides_form_alter where I successfully check for $form_id = 'mytype_node_form'
Question:
What should be the name of my validation function (hook_validate) in module form_overrides that would allow me to add custom validation to form mytype_node_form?
I thought it should look something like this
function form_overrides_mytype_node_form_validate($form, &$form_state) or
function form_overrides_validate($form, &$form_state)
If you don't specify the validation function in $form['#validate'], then Drupal looks for a function named with the form ID plus "_validate". So in this case it would be looking for mytype_node_form_validate() (and similarly mytype_node_form_submit() for the submit function).

Resources