Drupal 7以编程方式创建webform组件

时间:2013-11-10 23:44:28

标签: drupal-7 components drupal-webform

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

我正在尝试调整代码,以便它适用于现有的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);

当我尝试执行代码时,出现以下错误:

  

错误消息SQLSTATE [23000]:完整性约束违规:1048列'nid'不能为空

1 个答案:

答案 0 :(得分:2)

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

echo "<pre>";
print_r($node);
echo "</pre>"'
exit;

检查您是否获得了$node[nid]的价值。