Drupal 7 Drupal 7以编程方式创建Web表单组件

Drupal 7 Drupal 7以编程方式创建Web表单组件,drupal-7,components,drupal-webform,Drupal 7,Components,Drupal Webform,我遵循这一伟大的原则,成功地创建了一个webform和相关组件 我试图调整代码,使其适用于现有的webforms,而不是创建新的webforms。 我的代码如下: $nid = 12; $node = node_load(12); // Create the webform components. $components = array( array( 'name' => 'Gender', 'form_key' => 'gender'

我遵循这一伟大的原则,成功地创建了一个webform和相关组件

我试图调整代码,使其适用于现有的webforms,而不是创建新的webforms。 我的代码如下:

$nid = 12;

  $node = node_load(12);

  // Create the webform components.

  $components = array(

    array(

    'name' => 'Gender',

    'form_key' => 'gender',

    'type' => 'select',

    'mandatory' => 1,

    'weight' => 0,

    'pid' => 0,

    'extra' => array(

      'title_display' => 'inline',

      'private' => 0,

      'items' => "Mrs|Mrs\nMiss|Miss\nMr|Mr",

      'aslist' => 1,

    ),

  ),

  );

  // Setup notification email.

  $emails = array(

    array(

      'email' => 'somebody@example.tld',

      'subject' => 'default',

      'from_name' => 'default',

      'from_address' => 'default',

      'template' => 'default',

      'excluded_components' => array(),

    ),

  );

  // Attach the webform to the node.

  $node->webform = array(

    'confirmation' => '',

    'confirmation_format' => NULL,

    'redirect_url' => '<confirmation>',

    'status' => '1',

    'block' => '0',

    'teaser' => '0',

    'allow_draft' => '0',

    'auto_save' => '0',

    'submit_notice' => '1',

    'submit_text' => '',

    'submit_limit' => '-1', // User can submit more than once.

    'submit_interval' => '-1',

    'total_submit_limit' => '-1',

    'total_submit_interval' => '-1',

    'record_exists' => TRUE,

    'roles' => array(

      0 => '1', // Anonymous user can submit this webform.

    ),

    'emails' => $emails,

    'components' => $components,

  );

  // Save the node.

  node_save($node);
$nid=12;
$node=节点荷载(12);
//创建webform组件。
$components=数组(
排列(
'姓名'=>'性别',
“form_key”=>“性别”,
'类型'=>'选择',
“强制”=>1,
“权重”=>0,
“pid”=>0,
“额外”=>数组(
'标题显示'=>'内联',
“private”=>0,
'items'=>“Mrs | Mrs\nMiss | Miss\nMr | Mr”,
“aslist”=>1,
),
),
);
//安装通知电子邮件。
$emails=数组(
排列(
'电子邮件'=>'somebody@example.tld',
'主题'=>'默认',
'from_name'=>'default',
'发件人地址'=>'默认',
'模板'=>'默认',
“排除的组件”=>array(),
),
);
//将webform附加到节点。
$node->webform=array(
'确认'=>'',
“确认格式”=>NULL,
'重定向url'=>'',
“状态”=>“1”,
“块”=>“0”,
“摘要”=>“0”,
“允许草稿”=>“0”,
“自动保存”=>“0”,
“提交通知”=>“1”,
'提交文本'=>'',
'提交限制'=>'-1',//用户可以多次提交。
'提交间隔'=>'-1',
“总提交限额”=>“-1”,
“总提交间隔”=>“-1”,
“记录_存在”=>TRUE,
“角色”=>数组(
0=>“1”,//匿名用户可以提交此Web表单。
),
“电子邮件”=>$emails,
“组件”=>$components,
);
//保存节点。
节点\保存($node);
当我试图执行我的代码时,我得到以下错误:

错误消息SQLSTATE[23000]:完整性约束冲突:1048列“nid”不能为null


首先尝试打印$node的所有可用值

echo "<pre>";
print_r($node);
echo "</pre>"'
exit;
echo”“;
打印($node);
回声“'
出口

检查是否正在获取
$node[nid]
的值。

该节点确实存在