6

I added an extra submit-function to my form:

$submit = array('_check_value_public_catalog');
$form['actions']['submit']['#submit'] = array_merge($submit, $form['actions']['submit']['#submit']);

So the form looks like:

array
  0 => string '_check_value_public_catalogue'
  1 => string 'node_form_submit'

But I would like to pass some arguments to my _check_value_public_catalog function. How do I do this?

  • Are the arguments you wish to pass to your submit handler elements in the original form (i.e. results)? – Chapabu Jan 24 '12 at 12:49
  • @Chapabu, Jep, they are! – Michiel Jan 24 '12 at 13:10
9

You can't as such but you can store the values in the form somewhere and pick them up in your submit handler:

function mymodule_my_form($form, &$form_state) {
  $form['saved_value_1'] = array(
    '#type' => 'value',
    '#value' => 'The Value'
  );

  $form['#submit'][] = 'mymodule_my_form_submit';

  return $form;
}

function mymodule_my_form_submit(&$form, &$form_state) {
  $saved_value_1 = $form_state['values']['saved_value_1'];
}

Exactly the same principle applies if you're doing this in a form alter hook.

3

Form submission handlers don't use anymore custom parameters as reported in $form['#submit'] and $form['#validate'] and $form['#process'] no longer support custom parameters. In that page, which is for converting modules from Drupal 5 to Drupal 6, it is suggested to use $form['#custom_parameter'], where "#custom_parameter" is a string that doesn't conflict with one already used by Drupal.

function my_form() {
  // form definition code goes here...
  $form['#submit'][] = 'my_submit_function';
  $form['#my_form_param1'] = $param1;
  $form['#my_form_param2'] = $param2;
}

As alternative, the arguments passed to the form builder are available in $form_state['build_info']['args']. (See drupal_build_form(), which describes the values contained in $form_state.)
If the form builder is defined as in the following code, then $form_state['build_info']['args'] is an array containing "this is", and "a test".

$item['mymodule/test'] = array(
  'title' => 'Test',
  'description' => 'This is a test menu.', 
  'page callback' => 'drupal_get_form', 
  'page arguments' => array('node_admin_content', 'this is', 'a test'), 
  'access arguments' => array('access test pages'), 
);

In fact the code of drupal_get_form() is the following one:

function drupal_get_form($form_id) {
  $form_state = array();

  $args = func_get_args();
  // Remove $form_id from the arguments.
  array_shift($args);
  $form_state['build_info']['args'] = $args;

  return drupal_build_form($form_id, $form_state);
}

The function removes the first argument it gets, which is the form ID, and put the other arguments in $form_state['build_info']['args'].

3

Submit handlers have $form and $form_state automatically passed to them as arguments ($form_state will contain the submitted values for your form).

Assuming there are no arguments already supplied, your submit handler should look like below:

_check_value_public_catalog($form, $form_state) {
 //do funky stuff here
}

If you need specific arguments sent to the function, you could either write a different submit callback to pull the data out of $form_state and send it to your _check_value_public_catalog function, or re-write your submit function as below (before arguments are assumed).

Before:

function _check_value_public_catalog($foo, $bar) {
//cool stuff here with $foo and $bar
}

After:

function _check_value_public_catalog($form, $form_state) {
 $foo = $form_state['values']['foo'];
 $bar = $form_state['values']['bar'];

//cool stuff with $foo and $bar
}

See here for some more information.

1

found a good way to do this by attaching the parameters to the button:

when generating from

/**
* Implements hook_form_alter
*/
function MYMODULE_form_alter(&$form, $form_state, $form_id) {
  $nrbuttons = 10;
  for($i = 0; $i < $nrbuttons; $i++){
    $form['button_' . $i] = array(
      '#type' => 'submit',
      '#value' => t('button ' . $i),
      '#submit' => array('MYMODULE_form_submited'),
      '#attributes' => array(
         'button_index' => $i,
         'other_parameter'=>'value',
      ),
    );
  }
}

/**
* Custom submit handler
*/
function MYMODULE_form_submited($form, &$form_state){
    $i           = $form_state['clicked_button']['#attributes']['button_index'];
    $other_param = $form_state['clicked_button']['#attributes']['other_parameter'];
}
  • This is good way to achieve this. Small code, lots of buttons with values 'attached' to the buttons. Answers the OP and is flexible in other use cases. – jowan sebastian Jun 11 '15 at 17:56
  • Wow! I have not tried this out but does look brilliant to me. – GuruKay Oct 8 '17 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.