We have the ability to create dummy nodes through install_save_node(). And filling out some basic fields for those nodes is also no problem as long as they can be added to the node object like $node->field_name[0][value] - but filefields are a little bit trickier. Here's what I've tried out so far - though this is certainly not working yet. Maybe I could get a little help.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

EclipseGc’s picture

Try something like this:

/**
 * helper function
 */
function _my_widget_files_directory($field) {
  $widget_file_path = $field['widget']['file_path'];
  if (module_exists('token')) {
    global $user;
    $widget_file_path = token_replace($widget_file_path, 'user', $user);
  }
  return file_directory_path() .'\\'. $widget_file_path;
}

function my_node_import_function() {
  module_load_include('inc', 'filefield', 'field_file');
...

  $file = array();
    $nodetype = (object) content_types('node_type_here');
    if ($nodetype->fields) {
      foreach ($nodetype->fields as $field) {
        if ($field['type'] == 'filefield') {
          $field = content_fields($field['field_name'], 'report');
          $validators = filefield_widget_upload_validators($field);
          $files_path = _my_widget_files_directory($field);
          break;
        }
      }
    }
    $node->title = t('My title');
    $node->type = 'node_type_here';
    $node->$field['field_name'] = array(0 => $file);
    nodes_save($node);
...
}

Obviously this will not work for multiple filefields on the same node type, but w/ a little tweaking it could do that too.

Hope this pushes you in a helpful direction.

Eclipse

sirkitree’s picture

Ok, so this is working pretty well for one image, just have to get it working with two now...

sirkitree’s picture

Status: Needs work » Needs review
FileSize
2.14 KB

Ok, this cleans things up a bit. Makes multiple images work too!

There's one comment in there about doing something better.. namely switching between typecasts... I know there's a better way to do this just can't remember right now...

sirkitree’s picture

Sorry last patch was corrupt: try this one.

Also of note, there should be a more generalized way to do validation, similar to _content_field_invoke()