Domanda

I have successfully created a custom webform component for a specific type of data. The data comes in various sections so I need to create additional but existing component types which will be sub-components to my custom component. Basically, I am trying to create a composite of webform components programmatically.

My problem is, the code all executes successfully - I get my custom component gets created and I get feedback saying my sub-components were also successfully created. However, the sub-components do not show-up in my webform.

At the point when my custom component is created and inserted in to the DB, I am attempting to create all the necessary sub-components via the following code:

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']);
    }

  }
}

My _create_sub_component function is defined below:

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}");
  }

My guess is the call to node_save is causing the problem but I don't know exactly how.

È stato utile?

Soluzione

Got it!

Replace:

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

With:

webform_component_insert($component);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top