in mymodule_block_view I am generating some blocks for my site, depending on configuration data stored in the database. Some of these blocks may have Webform front ends:

$block = module_invoke('webform','block_view','client-block-' . $r['webform_nid']);

At the point above when I fetch the webform, I would then like to attach a custom submit hook to the webform. The submit hook will need access to the values entered into the webform by the user, and also some variables which are available to the block (so would need to be passed through to the webform somehow).

The submit hook needs to be selected dynamically by the block code, it won't be the same every time.

Any help much appreciated.


As Shawn says in his reply, webform_block_view returns a rendered webform, with no access to $form_state, so there is no way to pass information into the form. Instead, I figured out how to use drupal_build_form from within my module_block_view to generate the webform:

$node = node_load($webform_nid);
$form_state = array();
$form_state['build_info']['args'] = $node; 
$form_state['rebuild'] = FALSE;
$form_state['foo']['bar'] = $bas; // block specific info passed into form
$block['content'] = drupal_build_form($webform_name,$form_state);

I then added code into my module_form_alter as per his suggestion, to add the custom submit hook to the form. The submit hook then checks the foo and bar variables passed through from module_block_view and behaves accordingly.


One thing worth mentioning for anyone reading this page in future, is NOT to use $form['storage'] for storing custom form data when using webforms, as the webform module rather rudely destroys $form['storage']. This took me a long time to discover!

From _webform_client_form_pages_ in webform.module

// Determine what we need to do on the next page.
  if (!empty($form_state['save_draft']) || !$form_state['webform_completed']) {
    // Rebuild the form and display the current (on drafts) or next page.
    $form_state['rebuild'] = TRUE;
  else {
    // Remove the form state storage now that we're done with the pages.
    $form_state['rebuild'] = FALSE;

1 Answer 1


webform_block_view() returns a block object that already has the rendered webform. At that point, you're not going to be able to make any form altercations need for your custom submission handlers.

However, you can add a hook_form_alter() invocation to your custom module. Webform generates a standard form id for each form webform_client_form_NID.

MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'webform_client_form_NID') { //@TODO: substitute NID for your webform NID
    $form['#submit'][] = 'MYMODULE_webform_client_form_NID_submit';

If you need to pass data from your hook_block_view invocation to your hook_form_alter invocation, a global variable should help there.

  • Accepting this answer as it put me on the correct path for solving the issue. Many thanks! Sep 25, 2015 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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