【问题标题】:Drupal 7 programmatically create webform componentsDrupal 7 以编程方式创建 Web 表单组件
【发布时间】:2013-11-10 23:44:27
【问题描述】:

我关注了这个伟大的document,它成功地创建了一个网络表单和相关的组件。

我正在尝试调整代码,使其适用于现有的网络表单,而不是创建新的。 我的代码如下:

$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' 不能为空

【问题讨论】:

    标签: drupal-7 components drupal-webform


    【解决方案1】:

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

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

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

    【讨论】:

    • 节点肯定存在
    猜你喜欢
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2016-03-19
    • 1970-01-01
    相关资源
    最近更新 更多