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;
}