file_entity_test.pages.inc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * @file
  4. * Test pages for the File Entity Test module.
  5. */
  6. /**
  7. * Form callback; upload a file.
  8. */
  9. function file_entity_test_add_form($form, &$form_state) {
  10. $form['file'] = array(
  11. '#type' => 'managed_file',
  12. '#required' => TRUE,
  13. '#title' => 'File',
  14. '#upload_location' => 'public://',
  15. );
  16. $form['actions'] = array('#type' => 'actions');
  17. $form['actions']['submit'] = array(
  18. '#type' => 'submit',
  19. '#value' => t('Save'),
  20. );
  21. return $form;
  22. }
  23. /**
  24. * Form submit callback; save the uploaded file.
  25. */
  26. function file_entity_test_add_form_submit($form, &$form_state) {
  27. $file = file_load($form_state['values']['file']);
  28. if (!$file->status) {
  29. $file->status = FILE_STATUS_PERMANENT;
  30. file_save($file);
  31. }
  32. drupal_set_message(t('Your file has been saved.'));
  33. $form_state['redirect'] = 'file-entity-test/file/' . $file->fid;
  34. }
  35. /**
  36. * Page callback; view a file.
  37. */
  38. function file_entity_test_view_page($file) {
  39. return file_view($file, 'full');
  40. }
  41. /**
  42. * Page callback; preview a file.
  43. */
  44. function file_entity_test_preview_page($file) {
  45. return file_view($file, 'file_entity_test_preview');
  46. }
  47. /**
  48. * Form callback; edit a file.
  49. */
  50. function file_entity_test_edit_form($form, &$form_state, $file) {
  51. $form_state['file'] = $file;
  52. field_attach_form('file', $file, $form, $form_state);
  53. $form['file'] = file_view($file, 'file_entity_test_preview');
  54. // Add internal file properties needed by
  55. // file_entity_test_edit_form_validate().
  56. foreach (array('fid', 'type') as $key) {
  57. $form[$key] = array('#type' => 'value', '#value' => $file->$key);
  58. }
  59. $form['actions'] = array('#type' => 'actions');
  60. $form['actions']['submit'] = array(
  61. '#type' => 'submit',
  62. '#value' => t('Save'),
  63. );
  64. return $form;
  65. }
  66. /**
  67. * Form validation handler for the file edit form.
  68. */
  69. function file_entity_test_edit_form_validate($form, &$form_state) {
  70. entity_form_field_validate('file', $form, $form_state);
  71. }
  72. /**
  73. * Form submit handler for the file edit form
  74. */
  75. function file_entity_test_edit_form_submit($form, &$form_state) {
  76. $file = $form_state['file'];
  77. entity_form_submit_build_entity('file', $file, $form, $form_state);
  78. file_save($file);
  79. drupal_set_message(t('Your changes to the file have been saved.'));
  80. $form_state['redirect'] = 'file-entity-test/file/' . $file->fid;
  81. }