2

I have an existing webform in admin and I want to now render that form on other pages. I have a template page node--download_resource_protected.tpl that prints a hidden CSS pop-up box onto pages that show a teaser for content type Download Resource - Protected. When you click on the teaser, this pop-up shows. It currently includes a static form, asking for data-input before access to the download is allowed. See the screenshot here:

enter image description here

Now in place of this static form I want to render an existing webform with the API. The two teasers on the are defined as entity reference fields: Call to Action 1 and Call to Action 2.

After successful completion of the webform I want to post the data to the node URL of the teaser. For example if the page is displaying a teaser of node 34: the user clicks the teaser and the pop-up appears (this already happends), within the popup a webform is rendered (in place of the current static form) and on completion of the form it posts the data and loads URL node/34 (this needs to be dynamic i.e automatically directing to the correct node URL depending on node_id of the teaser).

I want to use hook_form_alter() to set a custom submit handler like this.

 if ($form_id == 'webform_client_form_112') {
    $form['#submit'][] = 'my_custom_submit_handler';
  }

But I need to pass the teaser node id in which the block webform was called, to this custom function (this will then allow me to to redirect to the node URL), any ideas?

Thanks for your time!

3
+50

This is how I'll do it:

  • Create the webform adding a hidden field with the default value of %get[teaser_node_id]
  • Load the webform with AJAX from the link node/webform_nid?teaser_node_id=teaser_nid

Then use hook_form_alter:

if ($form_id == 'webform_client_form_112') {
  $form['#submit'][] = 'my_custom_submit_handler';
}

Now in your submit function you'll know the teaser ID and know where to redirect the user

3

You can use the 'Available as block ' for webform settings. This option can find at 'form settings'->Advanced settings of your webform. Once it enabled you can render this webform block, instead of your static form as follows.

You can define custom submit handler for the webform, ex: $ form['#submit'][] = 'my_custom_submit_handler'; in form alter, in that function you can write the necessary code to save and redirect etc;

hope this may help you.

  • I know that I can render a webform as a block but I want to render it within node--download_resource_protected.tpl. So can I render the block here somehow? – Scott Lawrence Oct 1 '12 at 9:35
  • 1
    yes you can, this code is sample one which i embedded in my node--name.tpl.php and it working fine <?php $block = module_invoke('webform', 'block_view', 'client-block-4'); print render($block['content']);?> – Anoop Joseph Oct 1 '12 at 9:57
  • So I successfully have the form within the pop-up now. Could you expand the answer to explain how I define a custom submit handler please so that I can pass the $nid of the teaser in which the form renders? Thanks! @anoop-joseph – Scott Lawrence Oct 1 '12 at 20:22
  • You can add additional submit handler to a webform using form_alter. eg: function modulename_form_alter(&$form, &$form_state, $form_id) { if($form_id == 'webform_client_form_4') { //Note that the "submit" button may be in a different location depending on which version of Webform you're using (2.x or 3.x). $form['submit']['#submit'][] = 'mymodule_form_submit'; } } function mymodule_form_submit(){ //do your stuff here } – Anoop Joseph Oct 3 '12 at 4:06
  • Hey I realise that but I can't work out how to pass the node id of the teaser in which I rendered the webform, to the function – Scott Lawrence Oct 3 '12 at 7:29
0

You can pass node id through url and can access using $node_id = arg(1); from the submit function

  • The issue is that I need the node_id not of the page, but of the teaser which is a node_reference within the page. The form is loading with the page, so I don't know how I could use GET to pass anything through with a URL either. – Scott Lawrence Oct 3 '12 at 9:02
  • Unless I can apply the custom submit handler in node--download_resource_protected.tpl? Then maybe I could pass the teaser node in the function – Scott Lawrence Oct 3 '12 at 10:13

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.