title: Drupal webform programmatically creating composite components
body: <p>I have successfully created a custom webform component for a specific type of data. The data comes in various sections so I need to create additional but existing component types which will be sub-components to my custom component.
  Basically, I am trying to create a composite of webform components programmatically.</p>
  
  <p><strong>My problem is, the code all executes successfully - I get my custom component gets created and I get feedback saying my sub-components were also successfully created.
  However, the sub-components do not show-up in my webform.</strong></p>
  
  <p>At the point when my custom component is created and inserted in to the DB, I am attempting to create all the necessary sub-components via the following code:</p>
  
  <pre><code>function my_module_webform_component_insert($component){
    if($component['type'] == 'my_component'){
      $node = node_load($component['nid']);
  
      $address_fields = array("my_sub_component1", "my_sub_component2");
  
      foreach ($address_fields as $key =&gt; $address_field) {
          //fetch next available component ID
          $next_id_query = db_select('webform_component')-&gt;condition('nid', $component['nid']);
          $next_id_query-&gt;addExpression('MAX(cid) + 1', 'cid');
          $next_cid = $next_id_query-&gt;execute()-&gt;fetchField();
  
          _create_sub_component($component['nid'], $address_field, $next_cid, $component['cid']);
      }
  
    }
  }
  </code></pre>
  
  <p>My _create_sub_component function is defined below:</p>
  
  <pre><code>function _create_sub_component($nid, $new_component, $cid, $pid){
      $node = node_load($nid);
  
      $processed_name  = str_replace(' ', '_', strtolower($new_component));
      // Create the webform components array. Not sure if we need all these
      // values, but let's be sure.
      $component = array(
        'cid' =&gt; (int)$cid,
        'pid' =&gt; 0,#(int)$pid,
        'nid' =&gt;  (int)$node-&gt;nid,
        // I don't trust the admin to make a key based on input :)
        'form_key' =&gt; $processed_name,
        'name' =&gt; $new_component,
        // I want all lines to be numeric type component.
        'type' =&gt; 'textfield',
        'value' =&gt; '',
        'extra' =&gt; array(),
        'mandatory' =&gt; '0',
        'weight' =&gt; 0,
        'page_num' =&gt; 1,
      );
  
      array_push($node-&gt;webform['components'], $component);
      node_save($node);
  
      drupal_set_message("{$new_component} component successfully created in {$node-&gt;title}");
    }
  </code></pre>
  
  <p>My guess is the call to node_save is causing the problem but I don't know exactly how.</p>