file_module_test.module 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * @file
  4. * Provides File module pages for testing purposes.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function file_module_test_menu() {
  10. $items = array();
  11. $items['file/test'] = array(
  12. 'title' => 'Managed file test',
  13. 'page callback' => 'drupal_get_form',
  14. 'page arguments' => array('file_module_test_form'),
  15. 'access arguments' => array('access content'),
  16. );
  17. return $items;
  18. }
  19. /**
  20. * Form constructor for testing a 'managed_file' element.
  21. *
  22. * @see file_module_test_form_submit()
  23. * @ingroup forms
  24. */
  25. function file_module_test_form($form, &$form_state, $tree = TRUE, $extended = FALSE, $default_fid = NULL) {
  26. $form['#tree'] = (bool) $tree;
  27. $form['nested']['file'] = array(
  28. '#type' => 'managed_file',
  29. '#title' => t('Managed file'),
  30. '#upload_location' => 'public://test',
  31. '#progress_message' => t('Please wait...'),
  32. '#extended' => (bool) $extended,
  33. '#size' => 13,
  34. );
  35. if ($default_fid) {
  36. $form['nested']['file']['#default_value'] = $extended ? array('fid' => $default_fid) : $default_fid;
  37. }
  38. $form['textfield'] = array(
  39. '#type' => 'textfield',
  40. '#title' => t('Type a value and ensure it stays'),
  41. );
  42. $form['submit'] = array(
  43. '#type' => 'submit',
  44. '#value' => t('Save'),
  45. );
  46. return $form;
  47. }
  48. /**
  49. * Form submission handler for file_module_test_form().
  50. */
  51. function file_module_test_form_submit($form, &$form_state) {
  52. if ($form['#tree']) {
  53. $fid = $form['nested']['file']['#extended'] ? $form_state['values']['nested']['file']['fid'] : $form_state['values']['nested']['file'];
  54. }
  55. else {
  56. $fid = $form['nested']['file']['#extended'] ? $form_state['values']['file']['fid'] : $form_state['values']['file'];
  57. }
  58. drupal_set_message(t('The file id is %fid.', array('%fid' => $fid)));
  59. }
  60. /**
  61. * Implements hook_file_download().
  62. */
  63. function file_module_test_file_download($uri) {
  64. if (variable_get('file_module_test_grant_download_access')) {
  65. // Mimic what file_get_content_headers() would do if we had a full $file
  66. // object to pass to it.
  67. return array(
  68. 'Content-Type' => mime_header_encode(file_get_mimetype($uri)),
  69. 'Content-Length' => filesize($uri),
  70. 'Cache-Control' => 'private',
  71. );
  72. }
  73. }