1

特定のタイプのデータ用のカスタムWebフォームコンポーネントを正常に作成しました。データはさまざまなセクションにあるため、カスタムコンポーネントのサブコンポーネントとなる、追加の既存のコンポーネントタイプを作成する必要があります。基本的に、私はプログラムでWebフォームコンポーネントのコンポジットを作成しようとしています。

私の問題は、コードがすべて正常に実行されることです。カスタムコンポーネントが作成され、サブコンポーネントも正常に作成されたというフィードバックが表示されます。ただし、サブコンポーネントがWebフォームに表示されません。

カスタムコンポーネントが作成されてDBに挿入された時点で、次のコードを使用して必要なすべてのサブコンポーネントを作成しようとしています。

function my_module_webform_component_insert($component){
  if($component['type'] == 'my_component'){
    $node = node_load($component['nid']);

    $address_fields = array("my_sub_component1", "my_sub_component2");

    foreach ($address_fields as $key => $address_field) {
        //fetch next available component ID
        $next_id_query = db_select('webform_component')->condition('nid', $component['nid']);
        $next_id_query->addExpression('MAX(cid) + 1', 'cid');
        $next_cid = $next_id_query->execute()->fetchField();

        _create_sub_component($component['nid'], $address_field, $next_cid, $component['cid']);
    }

  }
}

私の_create_sub_component関数は次のように定義されています。

function _create_sub_component($nid, $new_component, $cid, $pid){
    $node = node_load($nid);

    $processed_name  = str_replace(' ', '_', strtolower($new_component));
    // Create the webform components array. Not sure if we need all these
    // values, but let's be sure.
    $component = array(
      'cid' => (int)$cid,
      'pid' => 0,#(int)$pid,
      'nid' =>  (int)$node->nid,
      // I don't trust the admin to make a key based on input :)
      'form_key' => $processed_name,
      'name' => $new_component,
      // I want all lines to be numeric type component.
      'type' => 'textfield',
      'value' => '',
      'extra' => array(),
      'mandatory' => '0',
      'weight' => 0,
      'page_num' => 1,
    );

    array_push($node->webform['components'], $component);
    node_save($node);

    drupal_set_message("{$new_component} component successfully created in {$node->title}");
  }

私の推測では、node_saveの呼び出しが問題を引き起こしていると思いますが、正確な方法はわかりません。

4

1 に答える 1

2

とった!

交換:

array_push($node->webform['components'], $component);
node_save($node)

と:

webform_component_insert($component);
于 2012-10-23T11:43:56.373 に答える