Issue #1349288 by danielnolde: Fixed devel generate has to create paths for filefields.
authorMoshe Weitzman
Tue, 22 Nov 2011 20:47:31 +0000 (15:47 -0500)
committerMoshe Weitzman
Tue, 22 Nov 2011 20:48:28 +0000 (15:48 -0500)
devel_generate/file.devel_generate.inc

index 4751ebe..84ed5ed 100644 (file)
@@ -14,22 +14,31 @@ function _file_devel_generate($object, $field, $instance, $bundle) {
 
   if (empty($file)) {
     if ($path = devel_generate_textfile()) {
+      $source = new stdClass();
       $source->uri = $path;
       $source->uid = 1; // TODO: randomize? use case specific.
       $source->filemime = 'text/plain';
       $source->filename = array_pop(explode("//", $path));
-      $destination = $field['settings']['uri_scheme'] . '://' . $instance['settings']['file_directory'] . '/' . basename($path);
-      $file = file_move($source, $destination);
+      $destination_dir = $field['settings']['uri_scheme'] . '://' . $instance['settings']['file_directory'];
+      file_prepare_directory($destination_dir, FILE_CREATE_DIRECTORY);
+      $destination = $destination_dir . '/' . basename($path);
+      $file = file_move($source, $destination, FILE_CREATE_DIRECTORY);
     }
     else {
       return FALSE;
     }
   }
-  $object_field['fid'] = $file->fid;
-  $object_field['display'] = $field['settings']['display_default'];
-  $object_field['description'] = devel_create_greeking(10);
+  if (!$file) {
+    // In case a previous file operation failed or no file is set, return FALSE
+    return FALSE;
+  }
+  else {
+    $object_field['fid'] = $file->fid;
+    $object_field['display'] = $field['settings']['display_default'];
+    $object_field['description'] = devel_create_greeking(10);
 
-  return $object_field;
+    return $object_field;
+  }
 }
 
 /**