file.devel_generate.inc 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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->uri = $path;
  15. $source->uid = 1; // TODO: randomize? use case specific.
  16. $source->filemime = 'text/plain';
  17. $destination = $field['settings']['uri_scheme'] . '://' . $instance['settings']['file_directory'] . '/' . basename($path);
  18. $file = file_move($source, $destination);
  19. }
  20. else {
  21. return FALSE;
  22. }
  23. }
  24. $object_field['fid'] = $file->fid;
  25. $object_field['display'] = $field['settings']['display_default'];
  26. $object_field['description'] = devel_create_greeking(10);
  27. return $object_field;
  28. }
  29. /**
  30. * Private function for generating a random text file.
  31. */
  32. function devel_generate_textfile($filesize = 1024) {
  33. if ($tmp_file = drupal_tempnam('temporary://', 'filefield_')) {
  34. $destination = $tmp_file . '.txt';
  35. file_unmanaged_move($tmp_file, $destination);
  36. $fp = fopen($destination, 'w');
  37. fwrite($fp, str_repeat('01', $filesize/2));
  38. fclose($fp);
  39. return $destination;
  40. }
  41. }