1

I'm using the Webform module's block on custom content type.

The webform has some text fields and I want to pass some data from my custom content type fields to webform fields.

I tried to add a default value by using tokens but it didn't work ([node:custom_content_type_custom_field] is shown on the available tokens but didn't work).

How can I pass field data from current (custom) node to webform block?

enter image description here

2 Answers 2

1

The only way to do this is using a hook_form_alter against the webform block. Webform is a great module overall but when you step outside of what it provides out of the box it gets complicated. The hook_form_alter will need to be set to only modify that form based on the form ID that gets created when the form is built.

The code will be similar to this:

/**
 * Implements hook_form_alter().
 */
function yourmodule_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'webform_client_form_103') {
  // Do your mods.
  }
}

NOTE: 'webform_client_form_103' will be replaced with your form id.

1
  • Thanks, actually I've tried form_alter method but didn't success on getting the custom field. Now, I'm able to do this as shown in my answer.
    – herci
    Commented Jul 31, 2015 at 7:51
1

I could do what I want by hook_form_alter but there is something else to be careful about.

I created a new custom module myform.

<?php
function myform_form_alter(&$form, &$form_state, $form_id) {


I want my custom module to effect only a specific webform:

  if ($form['#id'] == 'webform-client-form-2') {


I used menu_get_object() to load current node data.

    if ($node = menu_get_object()) {


Here is important, because the webform is available as a standalone page. If I don't use the code below it gives error on the webform page because there is no field_custom_field on that page.

        if($node->type == "custom_content_type") {


Then I passed the custom_field's value to webform field:

        $form['submitted']['my_form_field']['#default_value'] 
           = $node->field_custom_field['und'][0]['value'];


As a final step, I made the my_form_field read-only:

        $form['submitted']['my_form_field']['#disabled']=TRUE;


And this is the complete form of myform.module:

<?php
function myform_form_alter(&$form, &$form_state, $form_id) {
  if ($form['#id'] == 'webform-client-form-2') {
    if ($node = menu_get_object()) {
        if($node->type == "custom_content_type") {
        $form['submitted']['my_form_field']['#default_value'] 
           = $node->field_custom_field['und'][0]['value'];
        $form['submitted']['my_form_field']['#disabled']=TRUE;
    }
  }
 }
}


Bonus: You can set the CSS for the my_form_field to not show:

.webform-component--my-form-field {
    display: none;
}

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.