It would be really helpful if there was a programatic way to import from an export without using the UI - for example, when enabling a feature.

Here is how I had to do it:

    // create the node stub
    $node = new stdClass();
    $node->type = 'webform';
    $node->title = 'Contact Us';
    $node->status = 1;
    node_object_prepare($node);
    $node->language = 'und';
    node_save($node);

    // programatically execute the import form
    $file = file_get_contents(drupal_get_path('module', 'mymodule') . '/webform.txt');
    $form = drupal_get_form('webform_share_components_update_form', $node, 'import');
    $form_state['values']['components_only'] = 0;
    $form_state['values']['import'] = $file;
    $form_state['node'] = $node;
    webform_share_components_update_form_submit($form, $form_state);

Note that I could not use drupal_form_submit, because $form_state['node'] gets clobbered and becomes null, breaking the code. This was the only way I could do a reliable, repeatable import without relying on the nid in the export (I never want to do that) and I first check to see if the node exists, by title and type... just wish there was a more intuitive way to do this.

Comments

Alan D.’s picture

Status: Active » Needs review

Wouldn't this work?

$node->language = 'und';
$node->webform = _webform_share_eval(file_get_contents(drupal_get_path('module', 'mymodule') . '/webform.txt'));
node_save($node);

Edit: This is for new nodes only, it would fail if you tried updating existing webforms.

kevinquillen’s picture

This was the only way I could follow the code on what happens during import, I don't think I recall seeing _webform_share_evail().

Alan D.’s picture

very simple:

function _webform_share_eval($str) {
  eval($str);
  return empty($webform) ? FALSE : $webform;
}

did that work?

kevinquillen’s picture

Can't really check at the moment - this was in a feature .install file for a project that has launched.