How to write webform settings and conditions programmatically I'm trying to create a webform on my node programmatically so that I can set it up to be used as for a specific scenario. What I wanted to do is have all of the components my client want to appear automatically, but also default to specific settings for user entry, email specific users and validation rules etc, because they are going to have the same setup for each one, and they aren't going to want to write their setup out every single time.

I was using the Webform Default Fields module, but I know that this only works with the components, and not the setup. So I got rid of it and started writing my own version, with big help from this answer to get me as far as I am right now. But I'm struggling to find the correct format of how to write in a conditional statement, or how you would set the settings for the webform.

My current code is as follows:

function mymodule_node_insert($node) {
  if ($node-&gt;type == 'event') {
    _abstract_webform_components($node);
  }
}

function _abstract_webform_components($node) {
  $components = array(
      0 =&gt; array(
        'name' =&gt; 'First name',
        'form_key' =&gt; 'first_name',
        'type' =&gt; 'textfield',
        'mandatory' =&gt; 1,
        'weight' =&gt; 1,
        'pid' =&gt; 0,
        'extra' =&gt; array(
          'private' =&gt; 0,
        ),
      ),
      1 =&gt; array(
        'name' =&gt; 'Last name',
        'form_key' =&gt; 'name',
        'type' =&gt; 'textfield',
        'mandatory' =&gt; 1,
        'weight' =&gt; 2,
        'pid' =&gt; 0,
        'extra' =&gt; array(
          'private' =&gt; 0,
        ),
      ),
    );
      // etc etc

  webform_ensure_record($node);
  foreach($components as $component) {
    $component['nid'] = $node-&gt;nid;
    $component['extra']['title_display'] = 'inline';
    $component['extra']['locked'] = 1;
    $node-&gt;webform['components'][] = $component;
    webform_component_insert($component);
  }

  // Retrieves information about the author
  $author = user_load($node-&gt;uid);
  // Adds the author of the node as the default email recipient
  $email = array(
    'nid' =&gt; $node-&gt;nid,
    'email' =&gt; $author-&gt;mail,
    'subject' =&gt; 'default',
    'from_name' =&gt; 'default',
    'from_address' =&gt; 'default',
    'template' =&gt; 'default',
    'excluded_components' =&gt; array(),
  );
  webform_email_insert($email);

  $validation = array(
   'validator' =&gt; 'max_words',
   'action' =&gt; 'add',
   'nid' =&gt; $node-&gt;nid,
   'rulename' =&gt; 'Maximum Abstract Length',
   'rule_components' =&gt; array(
     1 =&gt; '1',
   ),
   'data' =&gt; 300,
  );
  webform_validation_rule_save($validation);

  }


Basically I'm missing the correct notation for how you add a condition to the webform but I have the right command to insert it. Also, I have the notation for how I want to setup my webform settings, but I don't know how to apply it

Many Thanks

EDIT

I've got one half of it, I've managed to add the conditional statement into the code, I was actually using the wrong code, but I managed to find a proper example of how the array needs to be written, edited the code above
I had a similar requirement :

The site will have some landing page contents and the site owner can add those contents at different times, and once a content of type landing_pages will be created some blocks (already created) will get enabled on that node details page and a new webform node with some predefined fields will get created and get enabled as a block on that page in particular places.

I did it in D6.

My approach for the dynamic webform block :


I created a webform node with all the default &amp; predefined fields, say with nid = 100
Enabled 'Available as block' option by visiting 'Form settings' tab in the path 'node/100/webform/configure'
Created a custom module 'landing_pages'
I cloned the nid = 100, each time a new landing_pages content is created
In hook_init() used this code :


/**
 *Implementation of hook_init(). 
 */
function landing_pages_init() {
// Update the 'blocks' DB table with the blocks currently exported by modules.
_block_rehash( $theme = NULL );
}

In hook_node_api() used this code :


/**
 *Implementation of hook_node_api().
 */
function landing_pages_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
 switch ($op) {
   case 'insert' :
     if ($node->type == 'landing_pages') {
       $node_id = 100;
       // function to create & save the clone node              
       $new_nid = clone_node_save_custom( $node_id, $node->title );
     }
   break;
   case 'load' :
     if ($node->type == 'landing_pages') {
       /*updating webform block*/
       $result2 = db_fetch_object( db_query( "SELECT n.nid , n.title FROM {node} n WHERE n.type = 'webform' ORDER BY n.nid DESC LIMIT 1" ) );
       $var_result2 = 'client-block-' . $result2->nid;
       $result3 = db_result( db_query( "SELECT b.bid FROM {blocks} b WHERE b.module = 'webform' ORDER BY b.bid DESC LIMIT 1" ) );
       $nid = $node->nid; // Can avoide this step & directly use $node->nid
       $var_result1 = 'node/' . $nid;
       $check1 = db_query( "SELECT bl.bid FROM {blocks} bl WHERE bl.pages LIKE '$nid' AND bl.module = 'webform'" );
       /*if blocks table doesn't have any entry for 'pages' like this 'nid'*/
       if (db_affected_rows() == 0) {
         $check2 = db_fetch_object( db_query( "SELECT b.bid, b.region, b.status, b.visibility, b.pages FROM {blocks} b WHERE b.delta LIKE '$var_result2'" ) );
         /*if blocks table doesn'thave any entry for 'delta' like '$var_result2'*/
         if (db_affected_rows() != 0) {
           /*if blocks table doesn't have any entry for 'region' & pages,0 as entry for 'status' & visibility*/
           if ($check2->region == '' && $check2->status == 0 && $check2->visibility == 0 && $check2->pages == '') {
             $exp = explode( '-#-', $result2->title );
             if ($exp[0] != '')
               $title = $exp[1];
               db_query( "UPDATE {blocks} b SET b.module = 'webform', b.delta = '%s', b.theme = 'YOUR_THEME_NAME', b.status = 1, b.region = 'sidebar_second', b.visibility = 1, b.pages = '%s', b.title = '%s' WHERE b.bid = %d", $var_result2, $var_result1, $title, $result3 );
             }
           }
         }
       }
    break;
    case 'delete' :
      if ($node->type == 'landing_pages') {
        $res = db_query( "SELECT b.bid, b.delta FROM {blocks} b WHERE b.module IN ('webform' , 'block') AND b.theme = 'YOUR_THEME_NAME' AND b.pages LIKE 'node/$node->nid'" );
        while ( $row = db_fetch_object( $res ) ) {
          if (! is_numeric( $row->delta )) {
            $exp = explode( 'client-block-', $row->delta );
            if ($exp[0] == '' && is_numeric( $exp[1] )) {
            //This validation can be skipped
              $type = db_result( db_query( "SELECT n.type FROM {node} n WHERE n.nid = %d", $exp[1] ) );
              if ($type == 'webform') {
                node_delete( $exp[1] );
            }
          }
        }
        else {
          db_query( "DELETE FROM {blocks} WHERE bid = %d", $row->bid );
          db_query( "DELETE FROM {boxes} WHERE bid = %d", $row->delta );
        }
      }
    }
    break;
 }
}

Supporting functions :

  
/**
 *Definition of clone_node_save_custom(). 
 */
function clone_node_save_custom($nid, $nodetitle) {
 if (is_numeric( $nid )) {
   global $user;
   $node = node_load( $nid );
   if (isset( $node->nid ) && clone_is_permitted( $node->type )) {
     $original_node = drupal_clone( $node );
     $node->nid = NULL;
     $node->vid = NULL;
     $node->tnid = NULL;
     $node->name = $user->name;
     $node->uid = $user->uid;
     $node->created = NULL;
     $node->menu = clone_node_clone_menu_link( $original_node );
     $node->book['mlid'] = NULL;
     $node->path = NULL;
     $node->files = array();
     $node->title = t( '!tit -#- !title', array('!tit' => $nodetitle, '!title' => $node->title ) );
     $node->clone_from_original_nid = $original_node->nid;

 if (variable_get( 'clone_reset_' . $node-&gt;type, FALSE )) {
   $node_options = variable_get( 'node_options_' . $node-&gt;type, array('status', 'promote' ) );
   foreach ( array('status', 'moderate', 'promote', 'sticky', 'revision' ) as $key ) {
     $node-&gt;$key = in_array( $key, $node_options );
   }
 }
 drupal_alter( "clone_node", $node, $original_node, "save-edit" );
 node_save( $node );
 return $node-&gt;nid;


  }
}
}



/**
*Defines clone_node_clone_menu_link()
*@param $node
*/
function clone_node_clone_menu_link($node) {
if (variable_get( 'clone_menu_links', FALSE )) {
  // This will fetch the existing menu link if the node had one.
  node_invoke_nodeapi( $node, 'prepare' );
  if (! empty( $node->menu['mlid'] )) {
    $old_link = $node->menu;
    $link['link_title'] = t( 'Clone of !title', array('!title' => $old_link['link_title'] ) );
    $link['plid'] = $old_link['plid'];
    $link['menu_name'] = $old_link['menu_name'];
    $link['weight'] = $old_link['weight'];
    return $link;
  }
}
return NULL;
}

Theme used is ZEN
Region I needed the webform be enabled is 'sidebar_second'
We shall have control over the settings of each webform for each Landing Page


Hope it helps.