Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal 7: Node fields in webform

I have a webform that appears in a block on content type 'job'. I am trying to get two fields from the node into the webform submission. I have this custom module:

function webform_nodevalues_form_alter(&$form, $form_state, $form_id) {  
  // 1. Webform ID  
  if ($form_id == 'webform_client_form_237') {  
    if ($node = menu_get_object()) {  
      // 2. Webform field for the node title  
      $form['submitted']['title']['#value'] = $node->title;  
      // 3. Webform field for a CCK field  
      $form['submitted']['email']['#value'] = $node->field_email[0]['value'];  
    }  }
}  

However, I'm getting this error:

Notice: Undefined offset: 0 in webform_nodevalues_form_alter() (line 35 of /drup/sites/all/modules/webform_nodevalues/webform_nodevalues.module).

Any thoughts on how to get the email field in the webform submission?

like image 858
MrPeanut Avatar asked May 17 '26 06:05

MrPeanut


2 Answers

I think this error because of the following line:

// ERROR HERE...
$form['submitted']['email']['#value'] = $node->field_email[0]['value'];

This should go like this:

$form['submitted']['email']['#value'] = $node->field_email['und'][0]['value'];

OR:

$form['submitted']['email']['#value'] = $node->field_email[LANGUAGE_NONE][0]['value'];

Hope this helps... Muhammad.

like image 190
Muhammad Reda Avatar answered May 19 '26 18:05

Muhammad Reda


A little more tinkering around and I figured it out (thanks to Muhammad Reda for pointing me in the right direction).

$form['submitted']['email']['#value'] = $node->field_email['und'][0]['email'];

I am new to Devel, but looking at the load, then just putting each level in brackets seemed to work.

like image 35
MrPeanut Avatar answered May 19 '26 18:05

MrPeanut