Web Form

Programmatically Create Web form Component Drupal 6

Easily create web form component programmatically (drupal 6).

//Field defination
$component_email = array(
    'type' => 'textfield',
    'nid' => $nodeid,
    'clone' => '',
    'name' => 'email',
    'form_key' => 'app_email',
    'extra' => Array
        (
            'title_display' => 'before',
            'private' => 0,
            'disabled' => 0,
            'unique' => 1,
            'conditional_operator' => '=',
        ),
    'cid' => 2,
    'required' => TRUE,
    'validate' => 'email',
    'mandatory' => 1,
    'weight' => 1,
    'value' => '',
);
AM_webform_comp_create($component_email);
/**
  * AM_webform_comp_create($component_email)
  *
  * Programmatically Create Webform Component
  *
  * Parameters: $component_email(array);
  *
  * Author: Mishu
  */
function AM_webform_comp_create( $component_email ) {
    //include webform.components form webform module
    module_load_include('inc', 'webform', 'includes/webform.components');
    //Get current node id
    if ( arg(0) == 'node' && is_numeric( arg(1) ) ) {
        $nodeid = arg(1);
    }
    //Form key exist or not
    $form_email_key_exist = db_result(db_query("SELECT form_key From webform_component WHERE nid='$nodeid' AND form_key='app_email'"));
    //If form key not exit insert the email field
    if(empty($form_email_key_exist)) {
        webform_component_insert($component_email_id);
    }
    //update webform table
    $tableName = 'webform';
    $data = array(
        'nid' => $node->nid,
        'confirmation' => '',
        'confirmation_format' => 1,
        'redirect_url' => '<confirmation>',
        'status' => 1,
        'block' => 0,
        'teaser' => 0,
        'allow_draft' => 0,
        'auto_save' => 0,
        'submit_notice' => 1,
        'submit_text' => '',
        'submit_limit' => -1,
        'submit_interval' => -1,
        'total_submit_limit ' => -1,
        'total_submit_interval ' => -1,
    );
    AM_dbInsert($tableName, $data);
    $tableName = 'webform_roles';
    $data = array(
        'nid' => $node->nid,
        'rid' => 1,
    );
    AM_dbInsert($tableName, $data);
    $tableName = 'webform_roles';
    $data = array(
        'nid' => $node->nid,
        'rid' => 2,
    );
    AM_dbInsert($tableName, $data);
}
/**
  * AM_dbInsert()
  *
  * Insert data into a table
  *
  * Parameters: $table(string), $data(array) | Example: $table='test'; $data = array( 'field_name' => 'value');
  *
  * Author: Mishu
  */
function AM_dbInsert($table, $data) {
    $field = implode(", ", array_keys($data));
    $values = "'" . implode("', '", array_values($data)) ."'";
    $tbfstfld = db_result(db_query("SHOW COLUMNS FROM {$table}"));
    $StudentPro = db_query("INSERT INTO {$table} ($field) values ($values)");
    if($StudentPro === true) {
        return db_last_insert_id($table, $tbfstfld);;
    }
}

Drupal web form component

It is very difficult to create web form component manually if you have a lot of content with web form.

 Drupal web form

twiIf you like to creating web form component programmatically or find any bug please leave me a comment. Hope you enjoy my Drupal web form post.