tripal_views_form_elements.inc 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * @file
  4. * Form elements used for tripal views
  5. */
  6. /**
  7. * Register form elements
  8. */
  9. function tripal_views_elements() {
  10. $type['file_upload_combo'] = array(
  11. '#input' => TRUE,
  12. '#process' => array('expand_file_upload_combo'),
  13. '#element_validate' => array('file_upload_combo_validate'),
  14. );
  15. return $type;
  16. }
  17. /**
  18. * Upload File and keep track of previously uploaded files
  19. * Form element description
  20. */
  21. function expand_file_upload_combo($element, $edit, $form_state, $complete_form) {
  22. if (empty($element['#value'])) {
  23. $element['#value'] = array(
  24. 'items' => '',
  25. 'items_file' => '',
  26. 'file_path' => '',
  27. );
  28. }
  29. $element['#tree'] = TRUE;
  30. $parents = $element['#parents'];
  31. $parents[] = 'items';
  32. $element['items'] = array(
  33. '#type' => 'textarea',
  34. '#default_value' => $element['#value']['items'],
  35. );
  36. $parents = $element['#parents'];
  37. $parents[] = 'items_file';
  38. $element['items_file'] = array(
  39. '#type' => 'file',
  40. '#title' => 'File upload',
  41. '#default_value' => $element['#value']['items_file'],
  42. );
  43. $parents = $element['#parents'];
  44. $parents[] = 'file_path';
  45. $element['file_path'] = array(
  46. '#type' => 'hidden',
  47. '#default_value' => $element['#value']['file_path'],
  48. );
  49. return $element;
  50. }
  51. /**
  52. * Theme the file upload combo form element
  53. */
  54. function theme_file_upload_combo($element) {
  55. return theme('form_element', $element, '<div class="container-inline">' . $element['#children'] . '</div>');
  56. }
  57. /**
  58. * Validate all content passed into the file upload combo form element
  59. */
  60. function file_upload_combo_validate($element, &$form) {
  61. $file = file_save_upload($element['#name'], array());
  62. if ($file) {
  63. $form['values'][$element['#name']]['file_path'] = $file->filepath;
  64. // we need to add our file path to the $_GET element as if it were
  65. // submitted along with the rest of the form
  66. $_GET[$element['#name']]['file_path'] = $file->filepath;
  67. }
  68. }