file.devel_generate.inc 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. function file_devel_generate($object, $field, $instance, $bundle) {
  3. if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
  4. return devel_generate_multiple('_file_devel_generate', $object, $field, $instance, $bundle);
  5. }
  6. else {
  7. return _file_devel_generate($object, $field, $instance, $bundle);
  8. }
  9. }
  10. function _file_devel_generate($object, $field, $instance, $bundle) {
  11. static $file;
  12. if (empty($file)) {
  13. if ($path = devel_generate_textfile()) {
  14. $source = new stdClass();
  15. $source->uri = $path;
  16. $source->uid = 1; // TODO: randomize? use case specific.
  17. $source->filemime = 'text/plain';
  18. $source->filename = basename($path);
  19. $destination_dir = $field['settings']['uri_scheme'] . '://' . $instance['settings']['file_directory'];
  20. file_prepare_directory($destination_dir, FILE_CREATE_DIRECTORY);
  21. $destination = $destination_dir . '/' . basename($path);
  22. $file = file_move($source, $destination, FILE_CREATE_DIRECTORY);
  23. }
  24. else {
  25. return FALSE;
  26. }
  27. }
  28. if (!$file) {
  29. // In case a previous file operation failed or no file is set, return FALSE
  30. return FALSE;
  31. }
  32. else {
  33. $object_field['fid'] = $file->fid;
  34. $object_field['display'] = $field['settings']['display_default'];
  35. $object_field['description'] = devel_create_greeking(10);
  36. return $object_field;
  37. }
  38. }
  39. /**
  40. * Private function for generating a random text file.
  41. */
  42. function devel_generate_textfile($filesize = 1024) {
  43. if ($tmp_file = drupal_tempnam('temporary://', 'filefield_')) {
  44. $destination = $tmp_file . '.txt';
  45. file_unmanaged_move($tmp_file, $destination);
  46. $fp = fopen($destination, 'w');
  47. fwrite($fp, str_repeat('01', $filesize/2));
  48. fclose($fp);
  49. return $destination;
  50. }
  51. }